C++ Examples

WAP in C++ to Input any number and print it in words

The Program in C++ to Input any number and print it in words is given below:

#include <iostream>
using namespace std;

string one_digit_number_to_word(int num) {
    switch(num) {
        case 0: return "zero";
        case 1: return "one";
        case 2: return "two";
        case 3: return "three";
        case 4: return "four";
        case 5: return "five";
        case 6: return "six";
        case 7: return "seven";
        case 8: return "eight";
        case 9: return "nine";
    }
    return "";
}

string two_digit_number_to_word(int num) {
    switch(num) {
        case 10: return "ten";
        case 11: return "eleven";
        case 12: return "twelve";
        case 13: return "thirteen";
        case 14: return "fourteen";
        case 15: return "fifteen";
        case 16: return "sixteen";
        case 17: return "seventeen";
        case 18: return "eighteen";
        case 19: return "nineteen";
    }

    string tens_digit[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    return tens_digit[num/10] + (num%10 != 0 ? "-" + one_digit_number_to_word(num%10) : "");
}

string three_digit_number_to_word(int num) {
    return one_digit_number_to_word(num/100) + " hundred " + (num%100 != 0 ? two_digit_number_to_word(num%100) : "");
}

int main() {
    int num;
    cout << "Hello Codeauri Family,enter the number here to print it into words:\n";
    cin >>num;

    if (num < 10) {
        cout << one_digit_number_to_word(num) << endl;
    } else if (num < 100) {
        cout << two_digit_number_to_word(num) << endl;
    } else if (num < 1000) {
        cout << three_digit_number_to_word(num) << endl;
    } else {
        cout << "Sorry, Number is too large.Please enter smaller than thhis one" << endl;
    }

    return 0;
}

Output:

Hello Codeauri Family,enter the number here to print it into words:
299
two hundred ninety-nine

Pro-Tips💡

This program prompts the user to enter a number and then prints the number in words.

It uses three different functions for one digit, two digits and three digit numbers.

The functions use switch case statements to match the digit with the corresponding string.

Then in the main function, it checks if the given number is less than 10,100,1000 and calls the corresponding function.

This program can handle numbers up to 999 and it will print “Number is too large” for any number greater than 999.

Note that this program only works for numbers between 0 and 999, and it doesn’t handle numbers with more digits.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X