QUESTION DESCRIPTION 

Indian Cricket Team needs the runs scored by some of the its biggest icons in wordings.

Since there are lot of greats such as Kapil Dev, Sachin, Dravid, Ganguly, Dhoni, it is very tough for the team management to convert their runs into wordings.

Can you help Indian Cricket team to automate this process??

Programming Language need to be used is:C++

Refer Sample test cases.


TEST CASE 1 

INPUT
12785
OUTPUT
twelve thousand seven hundred and eighty five

TEST CASE 2 

INPUT
10287
OUTPUT
ten thousand two hundred and eighty seven


#include<iostream>
using namespace std;
void expand(int);
int main()
{
    int num;
    cout<<"";
    cin>>num;
    expand(num);
}
void expand(int value)
{
    const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
    "eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
    "eighteen","nineteen"};
    const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
    "eighty","ninety"};

    if(value<0)
    {
        cout<<"minus ";
        expand(-value);
    }
    else if(value>=1000)
    {
        expand(value/1000);
        cout<<" thousand";
        if(value % 1000)
        {
            if(value % 1000 < 100)
            {
                cout << " and";
            }
            cout << " " ;
            expand(value % 1000);
        }
    }
    else if(value >= 100)
    {
        expand(value / 100);
        cout<<" hundred";
        if(value % 100)
        {
            cout << " and ";
            expand (value % 100);
        }
    }
    else if(value >= 20)
    {
        cout << tens[value / 10];
        if(value % 10)
        {
            cout << " ";
            expand(value % 10);
        }
    }
    else
    {
        cout<<ones[value];
    }
    return;
}

Automating the Conversion of Numbers to Words for Indian Cricket Legends

The Indian Cricket Team wants to convert the total runs scored by its legendary players into words. This task is quite challenging due to the large numbers involved, such as the runs scored by icons like Sachin Tendulkar or Kapil Dev. To help the team automate this process, we can write a C++ program that converts any given number into its word equivalent.

Problem Description

You need to write a C++ program that takes an integer input representing the total runs scored by a player and outputs the number in words. For example:

  • Input: 12785
  • Output: twelve thousand seven hundred and eighty-five

Breaking Down the Solution

To solve this problem, we will:

  1. Define arrays for the words corresponding to numbers (like "one", "two", etc.) and multiples of ten (like "twenty", "thirty", etc.).
  2. Create a function expand() that converts any number into words by:
    • Handling thousands, hundreds, tens, and units separately.
    • Recursively calling itself to break down the number into smaller parts.
    • Combining these parts into a coherent phrase.

How the Code Works

  1. Input Handling: The main() function reads an integer from the user.
  2. Number to Words Conversion:
    • The expand() function handles different ranges of the number:
      • Thousands: If the number is 1000 or more, it divides the number by 1000 to get the thousand's place, calls itself recursively to handle the remainder, and prints "thousand."
      • Hundreds: Similarly, for numbers 100 or more, it handles the hundreds and prints "hundred."
      • Tens and Units: For numbers between 20 and 99, it uses the tens array. For numbers less than 20, it directly prints the corresponding word from the ones array.
  3. Output: The number is output in words as a coherent English phrase.

Sample Test Cases

Test Case 1:

  • Input: 12785
  • Output: twelve thousand seven hundred and eighty-five

Test Case 2:

  • Input: 10287
  • Output: ten thousand two hundred and eighty-seven