hello sir..
I am not being able to make a program that accepts any number and displays it in words....
for example: when user enter 3456 then output should be three thousand four hundred fifty six.... 12 then print twelve.....9 then print nine...and so on....

Recommended Answers

All 5 Replies

First of all you didn't show any work,
the rule is that "you show the work and we help you with it"
it would be nice if you give it a go, and lets see where you get stuck.

First of all you didn't show any work,
the rule is that "you show the work and we help you with it"
it would be nice if you give it a go, and lets see where you get stuck.

sir I have tried it and it works except 10-19..... but look its too lengthy.... if I add 90 more lines then it will check for 10-19.... if there any other solution...?
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[15],i=0,no,t,c=0;
clrscr();
printf("enter any number");
scanf("%d",&no);
while(no!=0)
{
c++;
t=no%10;
arr[i++]=t;
no=no/10;
}
i--;
//printf("%d\n",c);
for(;i>=0;i-- )
{
switch(arr)
{
case 1:
if(c!=2)
printf("one\n");
if(c%4==0)
//printf("one\n");
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 2:
if(c!=2)
printf("two\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 3:
if(c!=2)
printf("three\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 4:
if(c!=2)
printf("four\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 5:
if(c!=2)
printf("five\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 6:
if(c!=2)
printf("six\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 7:
if(c!=2)
printf("seven\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 8:
if(c!=2)
printf("eight\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
case 9:
if(c!=2)
printf("nine\n");
if(c%4==0)
printf("thousand\n");
else if(c%3==0)
printf("hundred\n");
else if(c%2==0)
{
switch(arr[c-1])
{
case 2:
printf("twenty");
break;
case 3:
printf("thirty");
break;
case 4:
printf("forty");
break;
case 5:
printf("fifty");
break;
case 6:
printf("sixty");
break;
case 7:
printf("seventy");
break;
case 8:
printf("eighty");
break;
case 9:
printf("ninty");
break;
}
}
break;
}
c--;
}

getch();
}

I'd be tempted to use a small 2D word array:

powr[] array:
[0]="teen"
[1]="ty"
[2]="hundred"
[3]="thousand"
etc.
wrd[] array:
0="zero"
1="one"
2="two"
etc.

and this might easily change. I'm not familiar with this exercise.

and put together the string answer, from big to small number values:
ans[100]={""};
and use a small char array for the number, itself:
snum[10]={""};

You know how to "peel" off the digits, so that could be useful
As much as possible, I'd want to use the association of the pwr and wrd array index, with the right word, instead of long lines of explicit code. Go Loops! ;)

Some explicit code will be needed, I believe, but limit it.

Let me mess around with this a bit. Thinking along more representative lines of working with this problem, what do you see?

99% of the time, when you feel the code is just being pedantic as hell, you're right - it is, and it needs a better algorithm. You're there, I can tell. ;)

P.S: *IMPORTANT*
If you want people on the forum to study your code - highlight it, and click on the

icon at the top of the editing window.

Otherwise your code looks like html crap, and is very hard to even look at, let alone study. ALWAYS USE CODE TAGS around your code.[code]
icon at the top of the editing window.

Otherwise your code looks like html crap, and is very hard to even look at, let alone study. ALWAYS USE CODE TAGS around your code.

This isn't a full program, and it's VERY rough, but it shows the logic that would be hard to understand in pseudo code. I would use logic like this.

#include <stdio.h>
#include <string.h>

int main() {
  int i, j, digit, len, num, power; 
  char snum[10]={"1234"};
  char ans[100]={""};
  char pwr[4][10]={ {"one"}, {"ty"},{"hundred"},{"thousand"} };
  char wrd[20][20]={ {"zero"}, {"one"},{"two"},{"three"},{"four"},{"five"},
  {"six"},{"seven"},{"eight"},{"nine"},{"ten"},{"eleven"},{"twelve"},{"thirteen"},{"fourteen"},
  {"fifteen"},{"sixteen"},{"seventeen"},{"eighteen"},{"nineteen"} };
  // 012345678901234567890123456789012345678901234567890
  //(seventy seven thousand seven hundred, seventy seven) -- 50 char's
  // 15 spaces before thousands, 30 before hundreds

//  Marked out while debugging, only:
//  printf("\n\n Enter a number from 0 to 10,000: ");
//  scanf("%s", snum);
//  i = getchar();  
  
  printf("\nNumber is: %s", snum);
  len = strlen(snum);
  j=0;

  for(i=0;i<len;i++) {
    digit=snum[i]-'0';   //get the next digit from the string
    strcpy(&ans[j], &wrd[digit][0]);  //use it as an index to the wrd array
    j += strlen(wrd[digit]);       //adjust j;
    ans[j++]=' ';                //remove end of string marker char and j++

    //add the right power word here  //unfinished

  }
  printf("\n\n Answer: %s", ans);

  printf("\n\n\t\t\t     press enter when ready");

  i = getchar(); ++i;
  return 0;
}

Thanx sir..I will try it when I study about 2-d array...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.