Receive a number from the keyboard and tell you if it is divisible by 10, 5, 2 or if it is not
divisible by any of these.
Why this dont work?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
int num;
printf("number: ");
scanf("%d", &num);
if (num % 10 == 0 && num%5 == 0 && num % 2 == 0)
printf("The number is divisible by...\n");
else
printf("Not....\n");

return 0;
}

Recommended Answers

All 10 Replies

Use the OR operator instead of &&.

Also note:

if number is divisible by 10 ... it is ALSO divisable by 5 and 2 ... and it will end with (at least) one 0

if number is divisible by 5, it ends in 5 or 0
if it ends in 0, it is also divisible by 2 and 10 (if >=10)

if a number is divisable by 2 ... it will end in 0, 2, 4, 6 or 8 ... and will NOT be divisible by 5 or 10 UNLESS it ends in 0 ( and >= 10 )

so could test and report like this:

int flags[3] = {0}; /* get memory and initial all to 0 */

if( num % 2 == 0 ) flags[0] = 1;
if( num % 5 == 0 ) flags[1] = 1;
if( flags[0] && flags[1] ) flags[2] = 1; /* num%10==0 */

/* now can interpret and report flags */

First off , there is no need to use #include<stdlib.h> and #include<math.h> because there is no such need in your code.

i agree with both of the above replies.

Use the OR operator instead of &&.

correct. Use OR(||) operator and the problem will be solved.

if number is divisible by 10 ... it is ALSO divisable by 5 and 2 ...

absolutely correct.

but the op is saying " if it is not divisible by any of these". it means you the condition (num%10==0) will fail in case the value is 2,4,6 etc. So , if you want to check if number is either divisible by 2 or divisible by 4 or divisible by 6 then you have to use the following condition with OR(||) operator.

if(num%2==0 || num%5==0 || num%10==0) 
{
    print the message saying  "Number is either divisible by 2 or 5 or 10"
}

if you want to check whether the number is divisible by 2 AND 5 AND 10 then you just need this one :

if(num%2==0 && num%5==0)
{
    print the message saying "number is divisible by 2 And 5 And therefore 10 "
}

hello supermastereu. you write why this dont work?
what problem u got plz explain littlt bit. program runs fine.
10, 2 are even numbers so if any number is divisible by 10 then it also divisible by 2.
5 is prime.
if any number%2==0 then number is even.
so no need to num%10==0.
above post is right your coding is simple so no need

include<stdlib.h>
include<math.>

if you still got some problem please post problem in detail

i want to add more
add this in your coding after #include<stdio.h>

#include<conio.h>

and add this after return 0;
getch();

if got problem define

where is preview option to watch perview before post.
sorry guys there is little problem in both my above post
so correction is with full coding is.
sorry admin not did this in future..

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("number");
scanf("%d",&num);
if(num%2==0&&num%5==0)
{
  printf(" number is divisible\n");
}
else
{
printf(" not divisible\n");
}
getch();
}

run enter 50 answer is divsible. if enter 35 ans not divsible.

no need to add num%10==0

run enter 50 answer is divsible. if enter 35 ans not divsible.

if you read the question carefully then you would notice this (if it is not divisible by any of these.). That means you now need || rather than &&. therefore your statement which i quoted is absolute incorrect. then correct one is run enter 50 answer is divisible . if enter 35 ans divisible.

your statement which i quoted is absolute incorrect. then correct one is run enter 50 answer is divisible . if enter 35 ans divisible.

first of all thanks so much for checking. i accept my mstake
but mistake is only execution number i.e 35 replace it with 34
then answer is correct. coding is fine, no mistake there.

if you read the question carefully then you would notice this (if it is not divisible by any of these.)

wherea as i understood user requirement
user SUPERMASTEREU write if number is div by 2,5,10 print
nunber is div. if number is not div by 2,5,10 print not.
now bcoz number has to be div by all three given values(2,5,10)
so i and "user SUPERMASTEREU" use && operator in place of || operator.
on execution when i enter 50 number this is div by 2 ,5,10 with
all three numbers as per "user SUPERMASTEREU" requirement. if i enter
34 number then it fully div by 2 only not with 5,10. this is not
"user SUPERMASTEREU" reqirement. either div by all or not with all.

&& operator require all statements is to be true to answer true, if
single statement false answer is not true.

|| operator not require all statements is to be true to answer true, if
single statement false answer is still true.

plz post if i m wrong.
also where is preview button to watch preview before post

I would again say that read the thread again.I already pointed out the line , once again i do so ,

if it is not divisible by any of these.

focus on the string "any of these". do you get it now ?therefore || is used.

correct condition would be :

if(num%2==0 || num%5==0) 
{
    print the message saying  "Number is either divisible by 2 or 5 or 10"
}

now the if statement will get executed whenever the input is either divisible by 2 or 5.(number which is divisible by 10 must be divisible by 2 too.).therefore no need to put one extra condition like num%10==0.

your code would be preferrable if the condition all of these exist in the place of any of these. And your code will print not divisible if 15 in inputted. Hope you understand now.

where is preview button to watch preview before post

In comment Box , second last button(colored green).

yes u are write. i didn't read properly and miss the words "ANY OF THESE".
OR operator is to be used in place of AND operator. replace if(num%2==0 && num%5==0)
condition with if(num%2==0 || num%5==0) in my coding.
thanks for correcting me. also for perview button.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.