## Trending News

# Write a program to check whether the given number is divisible by 5 and not divisible by 10 by using if else statement. (C program)?

### 5 Answers

- Anonymous7 months ago
int n[6] = {4,5,6,9,10,11};

size_t size = sizeof(n) / sizeof(n[0]);

for (int i = 0; i < size; i++) {

if (0 == n[i] % 5 && 0 != n[i] % 10) {

printf("the given number %d is divisible by 5 and not divisible by 10", n[i]);

} else {

printf("the given number %d is not divisible by 5 or divisible by 10", n[i]);

}

printf("\n");

}

}

- Login to reply the answers

- husoskiLv 77 months ago
An earlier answer has the right idea, and it's probably good enough for your assignment.

One of the reasons for writing code is a "down-to-the-metal" language like C is to make code that runs faster and/or smaller. So, it's usually worth thinking about the shortest, and computationally simplest way to get a calculation done. Math helps a lot with that.

I'd describe the numbers you're testing for as the odd multiples of five: 5, 15, 25, ...; and if negative numbers can occur then include -5, -15, etc. Looking at the list, those are the number that end with 5 (in base ten, anyway). A more mathematical way of saying that is "numbers equivalent to 5, modulo 10".

If C's remainder operator (%) was truly a modulo operator, as many people like to call it, then the obvious test would work:

if (n % 10 == 5) { ... it's a multiple of 5, but not 10 ... }

Sadly, the % operator is NOT a modulo operator--not when n can be negative. It's a remainder operator; and the above test will fail if n == -15, since (-15) % 10 produces a remainder of -5, not 5. So, you can't test for a==b(mod c) with a%c == b%c. Instead, you need to test: (a-b)%c == 0.

if ((n - 5) % 10 == 0) {... it's an odd multiple of 5, even if n is negative ... }

If your goal is to be an application programmer, you probably won't have to worry about this kind of "micro optimization". Typically, you'll be minimizing time spent coding and debugging rather than making a program run faster or fit in a smaller ROM. And you probably won't be using C.

C is still important in an embedded system with limited hardware, where inefficient code can lead to needing more hardware and increase the cost of a the final product. And, this is the sort of thinking that can provide an edge in programming competitions--if you're into that sort of thing.

- Login to reply the answers

- Robert JLv 77 months ago
This would be the key section:

if(x % 5) == 0 && (x % 10) != 0) {

// The number is divisible by 5 but not 10

;

}

x is the variable containing the number being tested.

It uses the "modulus" operator "%" which gives the remainder of one number divided by another.

A remainder of zero means the first number is exactly divisible by the second.

- husoskiLv 77 months agoReport
Thumbs up, but % is not a modulo (or "modulus") operator in C/C++/Java/C#. It is in Python, though.

- Login to reply the answers

- How do you think about the answers? You can sign in to vote the answer.