1234567 1234567 - 1 year ago 77
C Question

Issue with a program in C to check if a number is divisible by 100

I wrote a program in C to check if the entered number is divisible by 100, but I've run into a problem. If I enter a number with 11 digits or more (with the last two digits being zeroes, of course), it says the number is not divisible by 100, even though it is. Help?

#include <stdio.h>
#include <conio.h>
int main()
long int a;
printf("Enter the number: ");
scanf("%d" , &a);
{printf("This number is divisible by 100");}
{printf("This number is not divisible by 100");}

Answer Source

Your number just doesn't fit into long int type, so the actual number you get is not what you expect. Try using unsigned long long, but be aware that numbers greater than 2^64 - 1 won't fit anyway. Also, you should use scanf("%llu", &a) in this case

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download