Write a program that converts numbers to words. For example, 895 results in "eight
nine five."

here are my codes:

#include<stdio.h>
#include<stdlib.h>
int main()
{
     printf(">>This program converts numbers to words :)\n");

    int value;

    printf("Enter value: ");
    scanf("%d",&value);

    switch(value){
        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;}

    }


    system("pause");
    return 1;
}

My problem here is that when i enter 4 the program disply four, but when i enter 45 there is nothnig displayed on the window!! and it should have display four five. Please help me with some explanation?

When you enter the number 45, what gets stored in the variable value?

Which of your switch cases handles that? Where is case 45: ?

Edited 3 Years Ago by Moschops

Inline Code Example Hereyes it is true, but how can i modify the codes for it to display four five with only these 10 cases.

Edited 3 Years Ago by nitish.mohiputlall: missed some information

Your switch statement only deals with numbers up to nine. As Moschops suggests, you need to deal with each digit of the number.

One easy way to do that, is to change the number, into a string of chars. Then deal with each digit in the array of chars.

Edited 3 Years Ago by Adak

The only problem is that in the lectures we have not yet done arrays. Up to now we have done if, if else,switch,for loop, while loop and do-while loop. I am somehow really stuck.

Then use getchar instead of scanf to read each digit as a character. Much easier:

#include <ctype.h>
#include <stdio.h>

int main(void)
{
    int ch;

    while (isdigit(ch = getchar()))
    {
        switch (ch)
        {
        case '0': fputs("zero ", stdout); break;
        case '1': fputs("one ", stdout); break;
        case '2': fputs("two ", stdout); break;
        case '3': fputs("three ", stdout); break;
        case '4': fputs("four ", stdout); break;
        case '5': fputs("five ", stdout); break;
        case '6': fputs("six ", stdout); break;
        case '7': fputs("seven ", stdout); break;
        case '8': fputs("eight ", stdout); break;
        case '9': fputs("nine ", stdout); break;
        }
    }

    putchar('\n');

    return 0;
}

Edited 3 Years Ago by deceptikon

Try This:

It will extract each digit before comparing.

#include<stdio.h>
#include<stdlib.h>
int main()
{
     printf(">>This program converts numbers to words :)\n");
    int value,rem;
    printf("Enter value: ");
    scanf("%d",&value);

    while(value>0)
    {
        rem=value%10;
        value=value/10;
        switch(rem){
        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;}
        }
    }
    system("pause");
    return 1;
}

It will extract each digit before comparing.

You introduced two bugs. One is easy to see and the other is a failure to account for edge cases. See if you can find them.

I see there it needed to be reveresed.

Yes, that's one. The other is what happens if you type nothing but 0? The output should be "zero", but nothing happens because your loop stops when value is 0.

Yes, that's one. The other is what happens if you type nothing but 0? The output should be "zero", but nothing happens because your loop stops when value is 0.

I replied that guy in PM with the corrected solutions.

Thanx for Noticing . :-)

This question has already been answered. Start a new discussion instead.