Printing an integer each digit at a time in english form recursively
How to get the digits of a number without converting it to a string/ char array?
This is for coding in C, not C++. My knowledge of C is very limited, because I'm in the entry level course, and we just got past midterm. Try to keep this as simple as possible, because I can't include keywords or operators that we haven't covered in class. I don't think this is necessary, since I think it's just my logic that needs help, not my code.
Having referenced the above two examples to write my code for a class, I am stumped as how to finish up my last little piece of the puzzle. I have found a couple of questions-answers here on SO that appeared to be relevant, however they used code to solve the problem that I have no knowledge on. Hopefully, someone can help me with my logic here.
My goal of my assignment is to:
Take a user defined integer, and display the digits in english. For example:
Please enter an integer: 123
You have entered: One Two Three
Then, I need to add up the sum of the digits (and if digits<10, display in english). In this case:
The sum of the individual digits is: Six
Finally, I need to then average the digits using 2 decimal places. In this case:
The average is: 2.00
I have ALL of this completed. Except: My first step lists the digits backwards! It reads 10s place, 100s place, 1000s place, etc. For example:
Please enter an integer: 123
You have entered: Three Two One
My conditions for this portion of the assignment, is that I may use only one switch statement, and I have to use a switch statement (meaning the need for a loop (I went with do)). I may also not use arrays. But finally, and most importantly, I may NOT reverse the input number (which was the solution to the the first version of this assignment). If I could do that, I would not be here.
Here is the excerpt of code that is relevant.
#include <stdio.h>
int main(void)
{
int userinput, digit
printf("Please input a number:");
scanf("%d", &userinput);
printf("You have entered: ");
if (userinput < 0)
{
printf("Negative ");
userinput = -userinput;
}
do
{
digit = userinput%10;
switch (digit)
{
case 0:
{
printf("Zero ");
break;
}
case 1:
{
printf("One ");
break;
}
case 2:
{
printf("Two ");
break;
}
case 3:
{
printf("Three ");
break;
}
case 4:
{
printf("Four ");
break;
}
case 5:
{
printf("Five ");
break;
}
case 6:
{
printf("Six ");
break;
}
case 7:
{
printf("Seven ");
break;
}
case 8:
{
printf("Eight ");
break;
}
case 9:
{
printf("Nine ");
break;
}
default:
{
break;
}
}
userinput = userinput/10;
} while (userinput > 0);
printf("\n");