This is regarding the problem which I posted earlier today. I made the program to identify and print the digits of a given number. The program runs fine when I use 1, 2, 4 digits (I made 4 max), but when I input a 3-digit number, it prints the numbers wrong and ends abruptly. Help me out..
#include <stdio.h>
#include <stdlib.h>
#include <digits.h>
int getDigit(long);
int main()
{
int ctr, digits, dig, multiNo, number;
printf("Enter the number(4 digits max): ");
scanf("%d", &number);
printf("\n");
digits = getDigit(number);
if (digits == 1)
printf("%d\n", number);
else
{
multiNo = pow(10, (digits-1));
ctr = 1;
dig = (number/multiNo);
printf("%d ", dig);
while (ctr < digits)
{
number %= multiNo;
multiNo/= 10;
dig = (number/multiNo);
printf("%d ", dig);
ctr++;
}
}
printf("\n\n");
system("PAUSE");
return 0;
}
int getDigit(long num)
{
long divider = 10;
int digit, i;
for (i=1; i<=9; i++)
{
digit = (num/divider);
if (digit == 0)
break;
else
divider *= 10;
}
return i;
}
Sorry, I'm using a phone to browse internet, so unable to format..