C Programming: How to Display Output from Numbers to Words?

Hello guys! I'm new in C programming and i want to know how to display out in words if number is inputed, using switch or an if..else statement. I have a little program sample but in my case the number inputed not would be greater than 9000.
Example:
if inputed is 168
The output will be "One Hundred-Sixty Eight"
I have a program but it only prints 1-10 ..T_T
Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>
void main()
{
int i=0;
unsigned long int digit;
char str[12],ch;
puts(“Enter the number (less than 10 digit)”);
scanf(“%lu”,&digit);
ultoa(digit,str,10); /*converts an unsigned long int to string*/
while(str[i]!=’\0′)
{
ch=str[i];
i++;
switch(ch)
{
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;
case ’0′:
printf(“ZERO “);
break;
}
}
}


That's the code i could only create but i need to know when big numbers is inputed and printing in words.Thanks!
This is C? You only know up to if and switch? Do you know loops? Can you use char arrays?
Not yet but just a little, but i forgot to add using a conditional statement..like if..else statement or switch..
Last edited on
I just realized you used all three items in the code. My fault.

Bigger question. Functions. Can you use them?
No just switch statement or an if else statement,,Functions.. ?, maybe but can functions do the program?..
Okay I really racked my brain on this one, an made a code that can do up to 9999. Its mainly in C++ and I rather not just give it to you. I will give you these insights.

* You do not need a char array, but you will need 5 integers.
* You do not need atoi, but you'll need to read a input as a integer.
* You will need a while loop, an understanding of division and a bunch of switches, if else, else if statements, and there will be nesting of these statements within each other. (about two ifs deep max)

Now the main logic:
You'll need to get the digits in the order you read them.
6302 <- Get 6 first, then 3, then 0, then 2. You'll need to divide by a large 10000, then 1000, then 100, then 10 , then 1. Make a while loop to make a number (I called this one divisor) an multiply it by 10 till its larger than the number you're calculating.

Then in another loop, you're going to need to divide by divisor, get both the remainder AND the front digit and store them. Don't store them over n.

Then you're going to make a bunch of if statements to calculate the correct words based on the front (the current digit you're working on.) When I wrote it the total I have is 5 switch statements inside 4 if statements + some minor if statements to end the loop if there's errors or nothing else to calculate.
1
2
3
std::string units[] = {"", "one", "two", "three", ...};
std::string dec[] = {"", "ten", "twenty", "thirty", "forty", ...};
std::string special[] = {"zero", "hundred", "thousand", "eleven", "twelve", ... , "nineteen"};

So you read a digit and use it as an index to figure out what to output.
You will need a special treat when the last two digits are numbers from 1 to 19.
Well ahhm can i use Loops? or just a string please help! need to decide..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int num, a,w,x,y,z;
printf("Enter a number: ");
scanf("%d", &num);
 
 
a=num/1000;
if (a==1)
   printf("one thousand");
else if (a == 2)
   printf("two thousand");
else if (a== 3)
   printf("three thousand");
else if (a == 4)
   printf("four thousand");
else if (a == 5)
   printf("five thousand");
else if (a==6)
   printf("six thousand");
else if (a == 7)
   printf("seven thousand");
else if (a == 8)
   printf("eight thousand");
else if (a == 9)
   printf("nine thousand");
 
y=num % 1000;
if (y >= 100 && y < 200)
   printf(" one hundred");
else if (y >= 200 && y < 300)
   printf(" two hundred");
else if (y >= 300 && y < 400)
   printf(" three hundred");
else if (y >= 400 && y < 500)
   printf(" four hundred");
else if (y >= 500 && y < 600)
   printf(" five hundred");
else if (y >= 600 && y < 700)
   printf(" six hundred");
else if (y >= 700 && y < 800)
   printf(" seven hundred");
else if (y >= 800 && y < 900)
   printf(" eight hundred");
else if (y >= 900 && y < 1000)
   printf(" nine hundred");
 
z=num%100;
if (z >= 20 && z < 30)
   printf( " twenty");
else if (z >= 30 && z < 40)
   printf( " thirty");
else if (z >= 40 && z < 50)
   printf( " forty");
else if (z >= 50 && z < 60)
   printf( " fifty");
else if (z >= 60 && z < 70)
   printf( " sixty");
else if (z >= 70 && z < 80)
   printf( " seventy");
else if (z >= 80 && z < 90)
   printf( " eighty");
else if (z >= 90 && z < 100)
   printf( " ninety");
else if (z >= 10 && z < 20)
  {w = z;}
 
if (w == 10)
   printf(" ten");
else if (w == 11)
   printf(" eleven");
else if (w ==12)
   printf(" twelve");
else if (w == 13)
   printf(" thirteen");
else if (w == 14)
   printf(" fourteen");
else if (w == 15)
   printf(" fifteen");
else if (w == 16)
   printf(" sixteen");
else if (w == 17)
   printf(" seventeen");
else if (w == 18)
   printf(" eighteen");
else if (w == 19)
   printf(" nineteen");
 
 
if (z < 10 || z >= 20)
   {x = z % 10;}
else if (x == 1)
   printf(" one");
else if (x == 2)
   printf(" two");
else if (x == 3)
   printf(" three");
else if (x == 4)
   printf(" four");
else if (x == 5)
   printf(" five");
else if (x == 6)
   printf(" six");
else if (x == 7)
   printf(" seven");
else if (x == 8)
   printf(" eight");
else if (x == 9)
   printf(" nine");
   
   getch();
   }


if i input 173
it only display "One hundred Seventy"
and so on..
Last edited on
Hello Guys Thank you for the codes specially ne55... ^_^
I want to share with you the code I've made using conditional statement..with guide of ne55..

here's my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <conio.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char *make_words(char *s, int ncomma);
char *insert_comma(long n, int *ncomma);
char *int2words(int n);
int a;
 
int main(void)
{
 


 printf("Enter number: ");
 scanf("%d",&a);
  printf("%d = %s\n",a,int2words(a));

 
  getch();  
  return 0;
}

char *make_words(char *s, int ncomma)
{
  int i, len, rest = 0;
  char *p = NULL;
  static char zzz[256];
 
  static char *ones[] = {"one ","two ","three ","four ",
    "five ","six ","seven ","eight ","nine "};
  static char *tens[] = {"ten ","eleven ","twelve ","thirteen ",
    "fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
  static char *twenties[] = {"","twenty ","thirty ","forty ",
    "fifty ","sixty ","seventy ","eighty ","ninety "};
  static char *hundreds[] = {
    "hundred ","thousand ","million "};
 
  memset(zzz, '\0', 256);  // fill with nulls
  len = strlen(s);
  for(i = 0; i < len; i++)
  {
 
    if ((p = strchr((s[i] == ',') ? &s[++i] : &s[i], ',')) == NULL)
    {
      p = &s[strlen(s)];
    }
    if (s[i] == '0')
    {
      continue;  // skip one iteration
    }
    if ((rest = (p - &s[i])) != 0)
    {
      if (rest == 3)
      {
        strcat(zzz, ones[s[i] - '0' - 1]);
        strcat(zzz, hundreds[0]);

        if (len == 7 && s[2] == '0')  strcat(zzz, hundreds[1]);
        if (len == 11 && s[2] == '0')  strcat(zzz, hundreds[2]);
      }
      else if (rest == 2) 
      {
        if (s[i] == '1')
        {
          strcat(zzz, tens[s[++i] - '0']);
          rest--;
        }
        else
        {
          strcat(zzz, twenties[s[i] - '0' - 1]);
        }
      }
      else
        strcat(zzz, ones[s[i] - '0' - 1]);
    }
    if (rest == 1 && ncomma != 0)
    {
      strcat(zzz, hundreds[ncomma--]);
    }
  }
  return zzz;
}
 

char *insert_comma(long n, int *ncomma)
{
  static char zzz[30];
  int i = 0;
  char *p = &zzz[sizeof(zzz)-1];
 
  *p = '\0';
  *ncomma = 0;
  do 
  {
    if (i % 3 == 0 && i != 0) 
    {
      *--p = ',';
      ++*ncomma;
    }
     *--p = (char)('0' + n % 10);
    n /= 10;
    i++;
  } while(n != 0);
  return p;
}
 
char *int2words(int n)
{
  int nc;
  char *ps, *zzz, *minus;
  char *buffer;
  buffer = (char *) malloc(256);
 
  // save any - sign
  if (n < 0)
  {
    minus = "minus";
    n = abs(n);
  }
  else
  {
    minus = "";
  }
 
  ps = insert_comma(n, &nc);

  zzz = make_words(ps, nc);
 
  sprintf(buffer,"%s %s", minus, zzz);
 
  return buffer;
  getch();
}



Thank You guys .. Marked as SOLVED!
Topic archived. No new replies allowed.