VOOZH about

URL: https://www.geeksforgeeks.org/cpp/print-individual-digits-as-words-without-using-if-or-switch/

⇱ Print individual digits as words without using if or switch - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print individual digits as words without using if or switch

Last Updated : 14 Dec, 2022

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 the input number be n. 

  1. Create an array of strings to store digit to word mapping.
  2. Create another array digits[] to store individual digits of n.
  3. 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.
  4. Traverse the digits array from end to beginning and print words using the mapping created in step 1.


Below is the implementation of the above idea. 


Output
three five zero 

Time Complexity: O(|n|), where |n| is the number of digits in the given number n.
Auxiliary Space: O(1), to store digit to word mapping


Thanks to Utkarsh Trivedi for suggesting above solution.

Comment
Article Tags: