The below program is to print the word representation of a number in CPP using loop.
Code:
#include <iostream.h> using namespace std; int main() { int num,x; int tot = 0; cout << "Enter the number: "; cin >> num; while(num>0) { x = num%10; tot = tot*10 + x; num = num/10; } while(tot>0) { x = tot%10; switch(x) { 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; case 0: cout << "ZERO "; break; default: cout << "INVALID INPUT "; break; } tot = tot/10; } return 0; } |
Output
Enter the number: 34786 THREE FOUR SEVEN EIGHT SIX |