Thread: Numbers In Words

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    22

    Numbers In Words

    Hi
    I have coded this code to print numbers up to 32767 in words.

    Well as a noob the job was tough for me.I want you all to read my code and tell me any flaw of it or a better logic to do it.

    Code:
    //	print numbers in word by 2d array
    
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
    char arr[10][6]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
    char arr1[10][8]={"","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    char arr2[10][10]={"","Eleven","Tewelv","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    int num;		//input by user
    int k[5]={0,0,0,0,0};   //each digit is stored in this to make there order correct.Max 5 digit 32767.Intialized to zero because no garbage is shown.
    int i;			//as usual for for loop
    int flag=0;             //for not print zero b4 any digit
    clrscr();
    scanf("%d",&num);
    printf("The number is %d\n",num);
    if (num==0)
    	printf("Zero");
    else
    {
    for(i=4;num>0;i--)
    {
        k[i]=num%10;
        num=num/10;
    }
    for(i=0;i<=4;i++)
      {
        if (k[i]>0 || flag==1)
         {
    	if (i==4)
    	    if(k[3]==1)
    	    printf("%s ",arr2[k[i]]);
    	    else
    	    printf("%s ",arr[k[i]]);  //k[i] will give digit from which arr[] will print its equ. word
    	else if (i==3)
    	    if(k[3]==1 && k[4]>0)
    	    {}
    	    else
    	    printf("%s ",arr1[k[i]]);
    	else if (i==2 && k[i]>0)
    	printf("%s Hundred ",arr[k[i]]);
    	else if (i==1)
    	{
    	    if (k[0]==1)
    	    {}
    	    else
    	    printf("%s ",arr[k[i]]);
    	printf("Thousand ");
    	}
    	else if (i==0)
    	{
    	    if (k[0]==1 && k[1]>0)
    	    printf("%s ",arr2[k[1]]);
    	    else
    	    printf("%s ",arr1[k[i]]);
    	flag=1;
    	}
         }
      }
    }
    getch();
    return 0;
    }
    If some one feel uncomfortable for understanding it feel free to ask.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Not a bad solution. Kudos. Here's another way to go about it that you can generalize for huge numbers more easily. Break the number down into groups of 3 digits. Handle printing up to 999 for those three digits, then walk from the most significant group to the least, printing the appropriate qualifier after each group--like billion, million, and thousand--until you don't have any groups left. That way you break the problem down into the simpler problem of printing up to 999. Handling the grouping is cake after that.

    When you see how easy it is, you'll wonder why you were restricted to 32767 in your exercise.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    22
    Actually i dont know the format string for double or float whatever is for integer value (as i have said i am noob) thats why this is restricted for max value a int can handle. Or else the code or the logic can support up to printing numbers upto 99999.
    by the way thanks for ur idea I will work on it.
    Last edited by year2038bug; 09-08-2005 at 10:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 09:34 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM