Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Input any number and print it in words

C++ For Loop: Exercise-60 with Solution

Write a program in C++ to input any number and print it in words.

Pictorial Presentation:

C++ Exercises: Input any number and print it in words

Sample Solution:-

C++ Code :

#include <iostream>
using namespace std;
int main()
{
    int n, num = 0, i;
    cout << "\n\n Print a number in words:\n";
    cout << "-----------------------------\n";
    cout << " Input any number: ";
    cin >> n;
    while (n != 0) {
        num = (num * 10) + (n % 10);
        n /= 10;
    }
    for (i = num; i > 0; i = i / 10) {

        switch (i % 10) {
        case 0:
            cout << "Zero ";
            break;
        case 1:
            cout << "One ";
            break;
        case 2:
            cout << "Two ";
            break;
        case 3:
            cout << "Three ";
            break;
        case 4:
            cout << "Four ";
            break;
        case 5:
            cout << "Five ";
            break;
        case 6:
            cout << "Six ";
            break;
        case 7:
            cout << "Seven ";
            break;
        case 8:
            cout << "Eight ";
            break;
        case 9:
            cout << "Nine ";
            break;
        }
    }
    cout << endl;
}

Sample Output:

 Print a number in words:                                              
-----------------------------                                          
 Input any number: 8309                                                
Eight Three Zero Nine

Flowchart:

Flowchart: Input any number and print it in words

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to find the frequency of each digit in a given integer.
Next: Write a program in C++ to print all ASCII character with their values.

What is the difficulty level of this exercise?