Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Consider what the primary problem is you are dealing with, you need to process the left most digit first, then the next to the right, then the next. But the math of using modulus and division goes from right to left. So what you need is some way to either save the math processing and reverse, or have the output be delayed. Two options are available.</p> <p>For an iterative approach you could utilize a FIFO queue type approach that holds the results of each digit and then prints out the queue. Could be as simple as an array with indexing:</p> <pre><code>int main(void) { int x, i; int result[32]; //arbitrary size int index = 0; printf("Enter the number you'd like converted to words\n"); scanf("%i", &amp;x); do { results[index++] = x % 10; x = x / 10; } while( index &lt; 32 &amp;&amp; x != 0 ); //now print in reverse order for(i = index-1; i &gt;= 0; i--) { switch (results[i]) { case 0: printf("zero\t"); break; case 1: printf("one\t"); break; case 2: printf("two\t"); break; case 3: printf("three\t"); break; case 4: printf("four\t"); break; case 5: printf("five\t"); break; case 6: printf("six\t"); break; case 7: printf("seven\t"); break; case 8: printf("eight\t"); break; case 9: printf("nine\t"); break; default: break; } } } </code></pre> <p>There is second approach that works which is recursive. Here you delay the printing of the output until you reach the left most digit. The built in stack is used for by the recursive calls.</p> <pre><code>void printNumbers(int x); int main(void) { int x; printf("Enter the number you'd like converted to words\n"); scanf("%i", &amp;x); printNumbers(x); } void printNumbers(int v) { if( v &gt; 9 ) { printNumbers( v / 10 ); } switch (v%10) { case 0: printf("zero\t"); break; case 1: printf("one\t"); break; case 2: printf("two\t"); break; case 3: printf("three\t"); break; case 4: printf("four\t"); break; case 5: printf("five\t"); break; case 6: printf("six\t"); break; case 7: printf("seven\t"); break; case 8: printf("eight\t"); break; case 9: printf("nine\t"); break; default: break; } } </code></pre> <p>Both approaches will solve the problem, but not if the input is a negative number.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload