Enter a Number and Print it into Words
Here you will get an example code of C++ program to enter a number and print it into words. This is the good example of nested if-else (conditional statement).
For example:
Input any number : 57
Fifty Seven
C++ Program to Enter a Number and Print it into Words
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 | #include<iostream> using namespace std; int main() { int num; string ones[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; string tens[] = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; string tw[] = {"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; cout << "Input any number(max two digits ) : "; cin >> num; if (num < 10) cout << ones[num]; else if (num < 20) cout << tw[num - 10]; else if (num < 100) { cout << tens[num / 10 - 1]; if (num % 10 != 0) cout << " " << ones[num % 10]; } else cout << "Invalid Number"; return 0; } |
Output 1
Input any number(max two digits ) : 78
Seventy Eight
Output 2
Input any number(max two digits ) : 18
Eighteen
Check out our other C++ programming Examples