C program help ?

Microsoft Visual studio 2008 professiona...
October 29, 2010 at 06:30:13
Specs: Linux i686
How to write A C program that asks the user to enter an integer from 25 to 95 ,then prints the english words of the number? what is the simple and easy way to do it ?

See More: C program help ?

Report •

#1
October 29, 2010 at 07:24:09
Probably pretty easy. If you meant "character" instead of "word," you can cast an int as a char, and you'll get the corresponding character.

If you meant "word," you'd probably need some sort of lookup table.

How To Ask Questions The Smart Way


Report •

#2
October 29, 2010 at 09:43:53
I did This so far :
#include <stdio.h>

main ()
{
    int number;
    int d1,d2;
    printf("Enter Your number(ranged from 25 to 95>\n");
    scanf("%d",&number);

    if (number>=25 && number <=95 ){

    d1=number/10;
    d2=number%10;


    switch (d2)
    {
        case '1':d2='One';
               break;
        case '2':d2='Two';
               break;
        case '3':d2='Three';
               break;
        case '4':d2='four';
               break;
        case '5':d2='Five';
               break;
        case '6':d2='Six';
               break;
        case '7':d2='Seven';
               break;
        case '8':d2='Eight';
               break;
        case '9':d2='Nine';
               break;

    }

    switch (d1)
    {
        case '1':d1='Ten';
               break;
        case '2':d1='Twenty';
               break;
        case '3':d1='Thirty';
               break;
        case '4':d1='Fourty';
               break;
        case '5':d1='Fifty';
               break;
        case '6':d1='Sixty';
               break;
        case '7':d1='Seventy';
               break;
        case '8':d1='Eighty';
               break;
        case '9':d1='Ninety';
               break;
    }

    }

printf("You Enterd this number : %d%d",d1,d2);
}

but the output is the same as the input :(
I.e
Enter your number :25
Your number is : 25

Report •

#3
October 29, 2010 at 10:30:02
You a defining d1 and d2 as integers and are then trying to allocate a string value. That is never going to work.

You need to define two string variable big enough to accept that largest word and uses that for the result.

Stuart


Report •

Related Solutions

#4
October 29, 2010 at 10:37:31
Using a lookup table would probably be easiest.

case '1':d2='One';
Three problems on this line:
1) Remove the single quotes around the digits. It's the difference between 1 and 49.
2) Use double quotes around "One" and not single quotes.
3) You cannot assign a cstring to an int

How To Ask Questions The Smart Way


Report •

#5
October 29, 2010 at 11:03:08
@Razor2.3
Thanks buddy

Three problems on this line:
1) Remove the single quotes around the digits. It's the difference between 1 and 49.
done
2) Use double quotes around "One" and not single quotes.
done
3) You cannot assign a cstring to an int
but here , what should I do ?
thanks again


Report •

#6
October 29, 2010 at 11:13:56
Since this is straight C, you'll need to use strcat(), found in string.h.
char output[256];
 . . . . . . .
case 1:strcat(output, "One");

EDIT: I should mention order counts; you should concatenate your 'tens' text before adding the 'ones' text.

How To Ask Questions The Smart Way


Report •

#7
October 29, 2010 at 11:36:06
Thank You Very Much , Now everything works very well.
and yes I've noticed that "tens" must be before "ones".

Report •

Ask Question