0

i want seprate 1234 like 1,2,3,4
when i give 1234 then this code works fine but if i give long int like 34545666 this code does not work.

int main()
{
 clrscr();
 int num,r,c,sp,x;
 printf("enter number");
 scanf("%d",&num);
 while(num!=0)
 {
 x=num%10;
 printf("%d",x);
 num=num/10;
 }
2
Contributors
1
Reply
10
Views
4 Years
Discussion Span
Last Post by deceptikon
0

My educated guess is that you're using Turbo C to run this program. Turbo C's int is 2 bytes rather than 4, so 34545666 is waaay out of range. Use a long int instead of int to ensure that the code works even on ancient compilers or compilers that target funky systems:

#include <stdio.h>

int main(void)
{
    long int num;

    printf("Enter a whole number: ");
    fflush(stdout);

    if (scanf("%ld", &num) == 1)
    {
        while (num != 0)
        {
            printf("%ld", num % 10);
            num /= 10;
        }

        puts("");
    }

    return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.