Print individual digits as words without using if or switch
Given a number, print words for individual digits. It is not allowed to use if or switch.
Examples:
Input: n = 123 Output: One Two Three Input: n = 350 Output: Three Five Zero
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to use an array of strings to store digit to word mappings. Below are steps.
Let input number be n.
- Create an array of strings to store digit to word mapping.
- Create another array digits[] to store individual digits of n.
- Traverse digits of n and store them in digits[]. Note that standard way of traversal by repeated storing n%10 and doing n = n/10, traverses digits in reverse order.
- Traverse the digits array from end to beginning and print words using the mapping created in step 1.
Below is the implementation of above idea.
C++
// C++ program to print individual words without if and // without switch #include <bits/stdc++.h> using namespace std; // To store digit to word mapping char word[][10] = {"zero", "one", "two", "three","four", "five", "six", "seven", "eight", "nine"}; void printWordsWithoutIfSwitch(int n) { // Store individual digits int digits[10]; // a 32 bit int has at-most 10 digits int dc = 0; // Initialize digit count for given number 'n' // The below loop stores individual digits of n in // reverse order. do-while is used to handle "0" input do { digits[dc] = n%10; n = n/10; dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (int i=dc-1; i>=0; i--) cout << word[digits[i]] << " "; } // Driver program int main() { int n = 350; printWordsWithoutIfSwitch(n); return 0; } |
chevron_right
filter_none
Java
// Java program to print individual words without // if and without switch class GFG { // To store digit to word mapping static String word[] = {"zero", "one", "two", "three","four", "five", "six", "seven", "eight", "nine"}; static void printWordsWithoutIfSwitch(int n) { // Store individual digits int digits[] = new int[10]; // a 32 bit int has at-most 10 digits int dc = 0; // Initialize digit count for given number 'n' // The below loop stores individual digits of n in // reverse order. do-while is used to handle "0" input do { digits[dc] = n % 10; n = n/10; dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (int i = dc - 1; i >= 0; i--) System.out.print(word[digits[i]] + " "); } // Driver program public static void main(String[] args) { int n = 350; printWordsWithoutIfSwitch(n); } } // This code has been contributed by 29AjayKumar |
chevron_right
filter_none
C#
// C# program to print individual words without // if and without switch using System; class GFG { // To store digit to word mapping static String []word = {"zero", "one", "two", "three","four", "five", "six", "seven", "eight", "nine"}; static void printWordsWithoutIfSwitch(int n) { // Store individual digits int []digits = new int[10]; // a 32 bit int has at-most 10 digits int dc = 0; // Initialize digit count for given number 'n' // The below loop stores individual digits of n in // reverse order. do-while is used to handle "0" input do { digits[dc] = n % 10; n = n/10; dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (int i = dc - 1; i >= 0; i--) Console.Write(word[digits[i]] + " "); } // Driver program public static void Main(String[] args) { int n = 350; printWordsWithoutIfSwitch(n); } } // This code contributed by Rajput-Ji |
chevron_right
filter_none
Python3
# Python program to prindividual words without if and # without switch # To store digit to word mapping word= ["zero", "one", "two", "three","four","five", "six", "seven", "eight", "nine"] def printWordsWithoutIfSwitch(n): # Store individual digits digits = [0 for i in range(10)] # a 32 bit has at-most 10 digits dc = 0 # Initialize digit count for given number 'n' # The below loop stores individual digits of n in # reverse order. do-while is used to handle "0" input while True: digits[dc] = n%10 n = n//10 dc += 1 if(n==0): break # Traverse individual digits and prwords using # word[][] for i in range(dc-1,-1,-1): print(word[digits[i]],end=" ") # Driver program n = 350printWordsWithoutIfSwitch(n) # This code is contributed by mohit kumar 29 |
chevron_right
filter_none
Output:
Three Five Zero
Thanks to Utkarsh Trivedi for suggesting above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- C++ program to print unique words in a file
- Find and print duplicate words in std::vector<string> using STL functions
- Print numbers with digits 0 and 1 only such that their sum is N
- Print first k digits of 1/n where n is a positive integer
- Print all numbers less than N with at-most 2 unique digits
- Print last k digits of a^b (a raised to power b)
- Print all n-digit numbers whose sum of digits equals to given sum
- Print a number containing K digits with digital root D
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Print a number strictly less than a given number such that all its digits are distinct.
- Switch Statement in C/C++
- Nested switch case
- Using range in switch case in C/C++
- Nested switch statement in C++
- Output of C programs | Set 30 (Switch Case)



