
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to convert all digits from the given range into words
Suppose we have two digits a and b. We shall have to convert each digit into words and print them one by one. Printing digits into words means for a digit 5, it should print "Five".
So, if the input is like a = 2, b = 6, then the output will be
Two Three Four Five Six
To solve this, we will follow these steps −
- 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")
- From the main method, do the following:
- for i in range a to be, do
- solve(i)
- move cursor to the next line
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 a = 2, b = 6; for(int i = a; i <= b; i++){ solve(i); cout << endl; } }
Input
2, 6
Output
Two Three Four Five Six
- Related Articles
- C program to write all digits into words using for loop
- C# Program to convert Digits to Words
- Convert given time into words in C++
- C++ program to convert digits to words using conditional statements
- C# program to remove all duplicates words from a given sentence
- Print all possible words from phone digits in C++
- C Program to convert a given number to words
- Java program to remove all duplicates words from a given sentence
- Program to print all palindromes in a given range in C++
- C program to convert digit to words
- Python Program to Replace all words except the given word
- Python Program to Remove all digits before given Number
- Golang Program to Read Three Digits and Print all Possible Combinations from the Digits
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- C++ Program to check if a given number is Lucky (all digits are different)

Advertisements