converting digits into words

This is a discussion on converting digits into words within the C Programming forums, part of the General Programming Boards category; Hi I have to make a program to generate the table of values from 0 to 9 It must have ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    converting digits into words

    Hi I have to make a program to generate the table of values from 0 to 9
    It must have 6 coloms with headings: Number, Double, Triple, Squared, Cubed and Words. I've done everything but last colomn, don't know how to convert digits into words and where to put that in a code. any advise?
    Also to have the right result inside of the functions I had to use (x+1) otherwise result was wrong, and I do not understand why?
    I know it must be simple because we had only 5 classes so far, but still can't make it.
    thanks in advance
    Code:
    // program to generate the tables of values from 0 to 9
    //no input from user
    
    #include<stdio.h>
    
    int x,a,b,c,d;
    int result;
    
    // functions
    int twice(int x)	//2 times the number
    {	
    	result=(x+1)*2;
    	return (result);
    }
    
    int triple(int x)	//3 times the number
    {	
    	result=(x+1)*3;
    	return(result);
    }
    
    int squared(x)		//number times itself
    {	
    	result=(x+1)*(x+1);
    	return(result);
    }
    int cubed(x)		//number times itself 3 times
    {
    	result=(x+1)*(x+1)*(x+1);
    	return (result);
    }
    
    main()
    { 
    	printf("Number\tDouble\tTriple\tSquared\t Cubed\t Words\n");
    	for(x=0; x<10; ++x)
    	{
    		printf("%3i%8i%8i%9i%9i\n", x, a, b, c, d);
    	a=twice(x);
    	b=triple(x);
    	c=squared(x);
    	d=cubed(x);
    	}
    
    	return 0;
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    First of all, why is x, a, b, c, d global variables? And especially, why is result a global variable?? x,a,b,c,d should be declared inside main and initialized to 0. And especially result should be declared inside every function

    Second, (and most important)look at this:
    Code:
    for(x=0; x<10; ++x)
    {
    	printf("%3i%8i%8i%9i%9i\n", x, a, b, c, d);
    	a=twice(x);
    	b=triple(x);
    	c=squared(x);
    	d=cubed(x);
    }
    You print and then calculate? That is why you do x+1. Put the printf after the functions and you should be ok.

    Third, indent properly before you post, as I did for you.

    Forth, and not important, here is a much slimmer version of your program, just to get an example how programs can be written in fewer lines. Don't try to do this as a bigginer, just keep this in mind:
    Code:
    // program to generate the tables of values from 0 to 9
    //no input from user
    
    #include<stdio.h>
    
    int twice(int x)
    {	
    	return x*2;
    }
    
    int triple(int x)
    {
    	return x*3;
    }
    
    int squared(x)
    {	
    	return x*x;
    }
    int cubed(x) 
    { 
            return x*x*x; 
    }
    
    main()
    { 
            int x;
    	printf("Number\tDouble\tTriple\tSquared\t Cubed\t Words\n");
    	for(x=0; x<10; ++x)
    		printf("%3i%8i%8i%9i%9i\n", x, twice(x), triple(x), squared(x), cubed(x););
    	return 0;
    }
    Saved 5 variables and some lines of code

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you, I like all your advise, and very helpfull.
    Code:
    for(x=0; x<10; ++x)
    		printf("&#37;3i%8i%8i%9i%9i\n", x, twice(x), triple(x), squared(x), cubed(x););
    return 0;
    We weren't shown how to do this, but I like it , I do not need to declare extra variables

    Still trying find out what the code to convert digits into words.
    Please excuse me if I'm typing incorrectly, just 5th year since I started to learn English

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Still trying find out what the code to convert digits into words.
    You'll need two character arrays as follows:

    The first array we'll classify as units. It will contain "zero", "one", "two", "three", thru "nineteen"

    The second array we'll classify as tens. It will contain "twenty", "thirty", thru "ninety"

    For starters, you will have to divide the input number by 100. If the resulting number is greater than zero, then use this resulting number as an index into the first units array to print the text at that index.

    Obviously, there is more code to complete the digits to text conversion. But this is a homework assignment. So, you'll have to put some effort into it. Post your questions if you get stuck

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Still trying find out what the code to convert digits into words.
    You'll need two character arrays as follows:

    The first array we'll classify as units. It will contain "zero", "one", "two", "three", thru "nineteen"

    The second array we'll classify as tens. It will contain "twenty", "thirty", thru "ninety"

    For starters, you will have to divide the input number by 100. If the resulting number is greater than zero, then use this resulting number as an index into the first units array to print the text at that index.

    Obviously, there is more code to complete the digits to text conversion. But this is a homework assignment. So, you'll have to put some effort into it. Post your questions if you get stuck.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Perhaps you may take a look at this.. []

    Code:
    #include <stdio.h>
    
    void convert(int n,char s[])
    {
     char *ones[]={"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ","Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
     char *tens[]={"","","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
     n>19 ? printf("&#37;s%s%s",tens[n/10],ones[n%10],s):printf("%s%s",ones[n],s);
    }
    
    int main(void)
    {
     long n=111100077;
    
     if(n>=10000000)
    	convert(n/10000000,"Crore ");
     if((n%10000000)/100000)
    	convert((n%10000000)/100000,"Lakh ");
     if((n%100000)/1000)
    	convert((n%100000)/1000,"Thousand ");
     if((n%1000)/100)
    	convert((n%1000)/100,"Hundred ");
     if(n%100)
    	convert(n%100,"");
     return 0;
    }
    You need to put restriction since it wont work for more than 999999999. I hope you will be able to do it

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    My problem was that I could not printf words under Words heading .
    I read all suggestions but since we did not learn arrays yet I was using if statement.
    The output is not what I expect, 'Zero , One etc. get prints in a first colomn where it must be in a in last one .

    Here is an output what I am getting:
    Code:
    Number  Double  Triple  Squared  Cubed   Words
    Zero  0       0       0        0        0
    One  1       2       3        1        1
    Two  2       4       6        4        8
    Three  3       6       9        9       27
    Four  4       8      12       16       64
    Five  5      10      15       25      125
    Six  6      12      18       36      216
    Seven  7      14      21       49      343
    Eight  8      16      24       64      512
    Nine  9      18      27       81      729
    Press any key to continue . . .
    Any advise what I have to do to fix it.

    Code:
    #include<stdio.h>
    
    // functions
    int twice(int x)	//2 times the number
    {	int result;
    	result=x*2;
    	return (result);
    }
    
    int triple(int x)	//3 times the number
    {	int result;
    	result=x*3;
    	return(result);
    }
    
    int squared(x)		//number times itself
    {	int result;		
    	result=x*x;
    	return(result);
    	
    }
    int words(x) //the number printed out(zero, one, etc)
    
    {
    	if(x==0)
    		printf("Zero");
    	if(x==1)
    		printf("One");
    	if(x==2)
    		printf("Two");
    	if(x==3)
    		printf("Three");
    	if(x==4)
    		printf("Four");
    	if(x==5)
    		printf("Five");
    	if(x==6)
    		printf("Six");
    	if(x==7)
    		printf("Seven");
    	if(x==8)
    		printf("Eight");
    	if(x==9)
    		printf("Nine");
    	return(x);
    }
    
    
    int cubed(x)		//number times itself 3 times
    { int result;
    	result=x*x*x;
    	return (result);
    }
    
    
    main()
    { int x;
    
    	
    	printf("Number\tDouble\tTriple\tSquared\t Cubed\t Words\n");
    
    	for(x=0; x<10; ++x)
    		printf("&#37;3i%8i%8i%9i%9i\n", x, twice(x), triple(x), squared(x), cubed(x), words(x));
    	
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    The output is not what I expect, 'Zero , One etc. get prints in a first colomn where it must be in a in last one
    It appears to me that the words function is being executed prior to the execution of the printf statement that has the words function in it. Thus, the printf in the words function is being executed prior to the execution of the printf in main. Thus, your "words" are printed prior to the numbers.

    An alternative:

    Code:
         for(x=0; x<10; ++x)
    	{
    		printf("%3i%8i%8i%9i%9i ", x, twice(x), triple(x), squared(x), cubed(x) );
    		words(x);
    		printf("\n");
    
              }
    What is the largest number that you will have to display as a word? I'm hoping it's not 729 as displayed in your post.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    What is the largest number that you will have to display as a word? I'm hoping it's not 729 as displayed in your post
    That is a simple table with numbers from 0 to 9

    Code:
    for(x=0; x<10; ++x)
    	{
    		printf("&#37;3i%8i%8i%9i%9i ", x, twice(x), triple(x), squared(x), cubed(x) );
    		words(x);
    		printf("\n");
    
              }
    I tried it before, it doesn' work. The "words" doesn't get printed at all . Pulling my hair out, what else can I do to make it printed.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course it works. Why do you think it doesn't?

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Well, this code:
    Code:
    #include<stdio.h>
    
    // functions
    int twice(int x)    //2 times the number
    {   
        int result;
        result=x*2;
        return (result);
    }
    
    int triple(int x)   //3 times the number
    {   
        int result;
        result=x*3;
        return(result);
    }
    
    int squared(int x)      //number times itself
    {   
        int result;     
        result=x*x;
        return(result);
    
    }
    int words( int x) //the number printed out(zero, one, etc)
    
    {
        if(x==0)
            printf("Zero");
        if(x==1)
            printf("One");
        if(x==2)
            printf("Two");
        if(x==3)
            printf("Three");
        if(x==4)
            printf("Four");
        if(x==5)
            printf("Five");
        if(x==6)
            printf("Six");
        if(x==7)
            printf("Seven");
        if(x==8)
            printf("Eight");
        if(x==9)
            printf("Nine");
        return(x);
    }
    
    int cubed(int x)        //number times itself 3 times
    { 
        int result;
        result=x*x*x;
        return (result);
    }
    
    main()
    { 
        int x;
        printf("Number\tDouble\tTriple\tSquared\t Cubed\t Words\n");
        for(x=0; x<10; ++x)
        {
            printf("%3i%8i%8i%9i%9i ", x, twice(x), triple(x), squared(x), cubed(x) );
            words(x);
            printf("\n");
        }
        return 0;
    }

    Gives me this output:

    Number Double Triple Squared Cubed Words
    0 0 0 0 0 Zero
    1 2 3 1 1 One
    2 4 6 4 8 Two
    3 6 9 9 27 Three
    4 8 12 16 64 Four
    5 10 15 25 125 Five
    6 12 18 36 216 Six
    7 14 21 49 343 Seven
    8 16 24 64 512 Eight
    9 18 27 81 729 Nine

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    Which is correct, except for the indentation.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    I found where I was wrong , I had "\n" in a code where it must not be.
    Code:
    for(x=0; x<10; ++x)
        {
            printf("&#37;3i%8i%8i%9i%9i\n, x, twice(x), triple(x), squared(x), cubed(x) );
            words(x);
            printf("\n");
    {
    Thanks again to all of you.
    Last edited by nynicue; 11-02-2008 at 10:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  2. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21