Q:

Write C++ program to print number in words

0

Write C++ program to print number in words

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
 
using namespace std;
 
int main()
{
    int n, num = 0;
 
    //Reading a number from user
    cout<<"Enter any number to print in words: ";
    cin>>n;
 
    while(n != 0)
    {
        num = (num * 10) + (n % 10);
        n /= 10;
    }
     //print corresponding digit in words till num becomes 0
    while(num != 0)
    {
        switch(num % 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;
        }
 
        num = num / 10;
    }
 
   return 0;
}

Result:

Enter any number to print in words: 12345

one two three four five 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to find HCF of two numbers... >>
<< Write a C++ program to check whether a number is p...