C++ Examples

WAP in C++ Program to Convert a given non-negative integer to English words

The Program in C++ Program to Convert a given non-negative integer to English words is given below:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
vector<string> tens = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
vector<string> teens = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};

string convert(int num)
{
    if (num >= 100) {
        return ones[num / 100] + " Hundred " + convert(num % 100);
    }
    else if (num >= 11 && num <= 19) {
        return teens[num - 11];
    }
    else if (num >= 10) {
        return tens[num / 10 - 1] + " " + convert(num % 10);
    }
    else if (num >= 1) {
        return ones[num];
    }
    else {
        return "";
    }
}

int main()
{
    int num;
    cout << "Hello Codeuari Family, Enter a non-negative integer to convert them into english words: ";
    cin >> num;

    cout << "Well, the English words of above non-negative integer is : " << convert(num) << endl;
    return 0;
}

Output:

Hello Codeuari Family, Enter a non-negative integer to convert them into english words: 999
Well, the English words of above non-negative integer is : Nine Hundred Ninety Nine

Pro-Tips💡

This program uses the convert function to convert a given non-negative integer num to English words.

The function uses several vectors to store the English words for ones, tens, and teens.

The function uses a series of if statements to check the number’s value and return the corresponding English words.

The main function takes an input integer from the user and calls the convert function to get the English words, then outputs the result.

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