void print(int num, int digits)
{
double mod, divi;
int mod1, divi1;
mod = pow(10, digits);
divi = pow(10, digits-1);
mod1 = (int)mod;
divi1 = (int)divi;
if(digits > 0)
{
printf("%d\n", ((num%mod1)/divi1));
digits--;
print(num, digits);
}
else
{
return;
}
}
This function is meant to print the digits from num
vertically, however when I run it the only thing it prints are 0's. I know it gets the correct number of digits because it prints the same number of zeroes as there are digits in the number, so something must be wrong with my use of Modulo or Division here, and I can't quite seem to find it. Any help would be appreciated thank you!
The function to get the numbers(maybe the problem is here?):
void getNum()
{
int num, digits=0;
printf("Enter a number to be printed vertically: ");
scanf("%d", &num);
if(num < 0)
{
digits = 1;
}
while(num)
{
num = num/10; //Here was the problem. Num was 0 once i passed it to print()
digits++; //Simply added a new variable to fix it. Thanks all!
}
print(num, digits);
}