1234567 1234567 - 13 days ago 7
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);
if(a%100==0)
{printf("This number is divisible by 100");}
else
{printf("This number is not divisible by 100");}
getch();
}

Answer

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