Hello everyone
Newbie here, I am self-learning C using Programming in C by Steve kochan, I am currently doing Chapter 6 exercise 6 for those that have worked with this book before,
Program simply takes a number from the user, and writes it out in words, (932 = nine three two), my logic when I wrote it was to have a loop reversed the number and then have another loop stracking the last digit using modulus and the print it out with a switch statement, so far so good.
But when I go and input number 0, because of the modulus operation it gets lost of course and it ends up not printing.
Here is my code
I know I can look up this problem online and find its solution but I am trying to avoid doing that until I finish it to compare my code and someone else,Code:// program that takes an integer and prints out the word #include <stdio.h> int main(void) { int userNumber, tempNumber, rightDigit, newNumber = 0; printf("Please enter any nonnegstive 3 digit number: "); scanf_s("%i", &userNumber); // ask for users input if (userNumber < 0 || userNumber > 999) { printf("Number is out of the range!\n"); } // checks for numbers outside of range do { // loop to reverse the number, ex 932 to 239 rightDigit = userNumber % 10; tempNumber = rightDigit; if (userNumber > 99 && userNumber < 1000) { tempNumber *= 100; newNumber += tempNumber; } else if (userNumber > 9 && userNumber < 100) { tempNumber *= 10; newNumber += tempNumber; } else if (userNumber >= 0 && userNumber < 10) { newNumber += tempNumber; } userNumber /= 10; } while (userNumber != 0); do { // loop to extrac the last digit and assigned the word in english for that number rightDigit = newNumber % 10; switch (rightDigit) { 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; case 0: printf("Zero"); break; default: break; } newNumber /= 10; } while (newNumber != 0); printf("\n"); return 0; }
Any help will be appreciated
Thanks