Integer o word

Hi, I am stuck of thinking how to convert integers to words using arrays. I cannot figure it out. Any help would be highly appreciated.

I would like to write a program without including switch command.

The problem is this: Write a program that takes an integer and displays the English name of that value.

You should support both positive and negative numbers, in the range supported by a 32-bit integer (approximately -2 billion to 2 billion).

My current code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

string num_to_text[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tens_to_text[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
string power_to_text[] = {"hundred", "thousand", "million", "billion" };

int main() {
  int number, division_by_thousand;
  printf("%s \n", "Print the number:");
  scanf("%d", &number);
  if (number < 9999)
  {
    if (number>1000)
    {
      division_by_thousand=number/1000;
      number=number%1000;
      printf("%s THOUSAND", num_to_text[division_by_thousand]);
    }
  }
}
Last edited on
closed account (48T7M4Gy)
It's forty, not fourty.
@kemort thanks.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using namespace std;

string num_to_text[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tens_to_text[] = {"","","twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
string power_to_text[] = {"hundred", "thousand", "million", "billion" };

int main() {
  int number = 21;
  int tens = number/10;
  cout << tens_to_text[tens] + ' ' + num_to_text[ number%10];
}
@ kemort thanks. That helps a lot. I'll try to figure the rest on my own and if I can't I'll post here.
closed account (48T7M4Gy)
Cheers @aurimas13, this exercise is very fiddly (negative numbers too) but the trick is to take it in small simple steps and test carefully as you go. Tip: address negative numbers at the end, that's the easy bit. :)
Last edited on
So I wrote the code up to 99999, but if I do until billion the code will become long and inefficient even tho correct. Therefore, I would like to ask what should I read about to make it more efficient or simply what I need to use? I know I can put everything in main to different functions, but that still would take a lot of space. In other words, what can I use to shorten the code using arrays? Thanks.

The code I have now to 99999 is this:

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
[#include <iostream>
#include <string>

using namespace std;

string num_to_text[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tens_to_text[] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
string power_to_text[] = {"", "hundred", "thousand", "million", "billion" };

int main() {
  int number, division_by_thousand;
  printf("%s \n", "Print the number:");
  scanf("%d", &number);
  if (number < 100 && number >= 1) { // 45
    int tens = number / 10; // 4
    int ones = number % 10; // 5
    if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
    else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
  }
  if (number < 1000 && number > 100) { //432
    int hundreds = number / 100; //4
    number %= 100; // 32
    int tens = number / 10; // 3
    int ones = number % 10; // 2
    cout << num_to_text[hundreds] + " " + power_to_text[1] + " ";
    if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
    else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
  }
  if (number > 1000 && number < 10000) { //1256; 9831
    int thousands = number / 1000; //1, 9
    cout << num_to_text[thousands] + " " + power_to_text[2] + " ";
    number %= 1000; // 256, 831
    int hundreds = number / 100; // 2, 8
    cout << num_to_text[hundreds] + " " + power_to_text[1] + " ";
    number %= 100;  // 56, 31
    int tens = number / 10; // 5, 3
    int ones = number % 10; // 6, 1
    if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
    else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
  }
  if (number >= 10000 & number < 100000){ // 99843
      int thousands = number / 1000; // 99
      int ten_thousands = number / 10000; // 9
      int ten_ones = thousands % 10; // 9
      if (thousands >= 1 && thousands < 20) { cout << num_to_text[thousands] + " " + power_to_text[2] + " "; }
      else { cout << tens_to_text[ten_thousands] + " " + num_to_text[ten_ones] + " " + power_to_text[2] + " "; }
      number %= 1000; // 843
      int hundreds = number / 100; // 8
      number %= 100; // 43
      int tens = number / 10; // 4
      int ones = number % 10; // 3
      if (number < 1) { cout << "";}
      else { cout << num_to_text[hundreds] + " " + power_to_text[1] + " "; }
      if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
      else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
  }
}
Last edited on
Instead of using int number;, you use long long number;
Does that help? :)
closed account (48T7M4Gy)
uint32_t via <cstdint> check it out.

As far as a better way, one is to convert the number by repeated modulo 10 operations to get an array of numbers which represent how many billions, hundreds of millions ... units. Then convert each member with the appropriate descriptor + connector phrase.

You have to look carefully at what you have done so far and see that 100's whether it's thousands, millions or whatever are the same.

TIP: You can just about forget if statements except for -ve numbers. I didn't need any.
@closed account

That isn't the case, but thank you. I'm more interested in knowing what should I change to make a more efficient code.

@kemort

Can you give me an example what you mean for both paragraphs? Thank you.
Last edited on
closed account (48T7M4Gy)
All you do is expand what I have given you so that the number is broken down completely. The next group is hundreds, then thousands ... and store the answers via separate variables.

Once the number is broken down completely take each group number and convert it.

It's not rocket science, just an extension of what's already there. If you can convert 250 then you can convert 250,000 250,000,00 etc with the greatest of ease. Just change the descriptor to thousand or million :)
Topic archived. No new replies allowed.