C++ program to convert digits to words using conditional statements



Suppose we have a digit d, we shall have to convert it into words. So if d = 9, our output should be "Nine". If we provide some d which is beyond the range of 0 and 9, it will return appropriate output.

So, if the input is like d = 3, then the output will be "Three".

To solve this, we will follow these steps −

  • Define a function solve(), this will take d,
  • if d < 0 and d > 9, then:
    • return ("Beyond range of 0 - 9")
  • otherwise when d is same as 0, then:
    • return ("Zero")
  • otherwise when d is same as 1, then:
    • return ("One")
  • otherwise when d is same as 2, then:
    • return ("Two")
  • otherwise when d is same as 3, then:
    • return ("Three")
  • otherwise when d is same as 4, then:
    • return ("Four")
  • otherwise when d is same as 5, then:
    • return ("Five")
  • otherwise when d is same as 6, then:
    • return ("Six")
  • otherwise when d is same as 7, then:
    • return ("Seven")
  • otherwise when d is same as 8, then:
    • return ("Eight")
  • otherwise when d is same as 9, then:
    • return ("Nine")

Example

Let us see the following implementation to get better understanding −

#include <iostream> using namespace std; void solve(int d){     if(d < 0 || d > 9){         cout << "Beyond range of 0 - 9";     }else if(d == 0){         cout << "Zero";     }else if(d == 1){         cout << "One";     }else if(d == 2){         cout << "Two";     }else if(d == 3){         cout << "Three";     }else if(d == 4){         cout << "Four";     }else if(d == 5){         cout << "Five";     }else if(d == 6){         cout << "Six";     }else if(d == 7){         cout << "Seven";     }else if(d == 8){         cout << "Eight";     }else if(d == 9){         cout << "Nine";     } } int main(){    int d;    cin >> d;    solve(d); } 

Input

3

Output

Three
Updated on: 2021-10-07T07:20:49+05:30

988 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements