### number to word code help-not for beginner apparently

I wasn't sure if this was best suited for general or for beginner.

Hello, the purpose of this program is to have a user input a number, and the program will then read out the number in words. Example: User inputs 10004, you get one zero zero zero four.

Also, any leading zero should be ignored. user inputs 00001, you get one.

Looking at the final for loop with the switch statement, part of my problem from setting up how the loop and the switch statement interact.

Any hints or ideas?

---------------

#include <stdio.h>

int main(){

int n,m,i,j,k,temp,temp2,numdigits; //add all the variables!

printf("Enter an integer no bigger than 10 digits and not starting with 0: ");//The only easy part of this program
scanf("%d",&n);

numdigits=0; //begin the count here
m=n;
for(j=0;m>0;j++)//counting the number of digits
{
m=m/10;//Divide m, initilized as n, by 10. Keep going until m<=0
numdigits=j+1;//numdigits: adds one to the count each time the loop runs.
}
HERE IS WHERE THE PROBLEM BEGINS
m=n;//re-initialize m as n
for (i=numdigits-1;i>=0;i--)
{
temp=0;
for (k=numdigits-1;k>=0;k--)
{
temp=temp*10;
temp2=n%10;
temp=temp+temp2;
m=temp2%10;
}

switch (m){
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 ");
default:
printf("error!");
}
}
return 0;
}
Last edited on
 ``12345678910111213141516171819202122232425262728`` ``````#include #include void print(int n) { static const char* digit_rep [] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; if (n) { print(n / 10); printf("%s ", digit_rep[abs(n%10)]); } } int main() { int n; printf("Enter an integer: "); scanf("%d", &n); if (n) print(n); else printf("zero\n"); }``````