to check whether the double no is divisible by 100 or not.

Mar 21, 2012 at 12:26pm
Im trying to execute the below program,but im getting error...
can anyone help me to solve this problem...

#include<stdio.h>
int main()
{
double x;
scanf("%lf",&x);
if((x % 100) == 0)
printf("%lf is divisible by 100",x);
else
printf("%lf is not divisible by 100",x);
return 0;
}
Mar 21, 2012 at 12:53pm
i think c doesn't like (double % int) ^^

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main()
{
int x = 0;
scanf("%d",&x);
if((x % 100) == 0)
printf("%lf is divisible by 100",x);
else
printf("%lf is not divisible by 100",x);
return 0;
} 


regards
Mar 21, 2012 at 1:45pm
You cannot use the '%' operator on double and float.
You may want cast it to an int, or to a long, like this:
1
2
3
4
5
6
7
8
9
10
int main()
{
	double x = 0;
	scanf("%lf",&x);
	if ( (((long)x) % 100) == 0)
		printf("%lf is divisible by 100",x);
	else
		printf("%lf is not divisible by 100",x);
	return 0;
}
Last edited on Mar 21, 2012 at 1:46pm
Mar 21, 2012 at 2:08pm
Why not multiply by 10 (or however many decimal places you have times 100, 1000, etc, depending on how many decimal points there are) and then divide by 100. If the answer is even, then it divides evenly.
Last edited on Mar 21, 2012 at 2:10pm
Topic archived. No new replies allowed.