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.
- 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 the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
char word[][10] = { "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" };
void printWordsWithoutIfSwitch( int n)
{
int digits[10];
int dc = 0;
do
{
digits[dc] = n%10;
n = n/10;
dc++;
} while (n != 0);
for ( int i=dc-1; i>=0; i--)
cout << word[digits[i]] << " " ;
}
int main()
{
int n = 350;
printWordsWithoutIfSwitch(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static String word[] = { "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" };
static void printWordsWithoutIfSwitch( int n)
{
int digits[] = new int [ 10 ];
int dc = 0 ;
do
{
digits[dc] = n % 10 ;
n = n/ 10 ;
dc++;
} while (n != 0 );
for ( int i = dc - 1 ; i >= 0 ; i--)
System.out.print(word[digits[i]] + " " );
}
public static void main(String[] args)
{
int n = 350 ;
printWordsWithoutIfSwitch(n);
}
}
|
C#
using System;
class GFG
{
static String []word = { "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" };
static void printWordsWithoutIfSwitch( int n)
{
int []digits = new int [10];
int dc = 0;
do
{
digits[dc] = n % 10;
n = n/10;
dc++;
} while (n != 0);
for ( int i = dc - 1; i >= 0; i--)
Console.Write(word[digits[i]] + " " );
}
public static void Main(String[] args)
{
int n = 350;
printWordsWithoutIfSwitch(n);
}
}
|
Python3
word = [ "zero" , "one" , "two" , "three" , "four" , "five" ,
"six" , "seven" , "eight" , "nine" ]
def printWordsWithoutIfSwitch(n):
digits = [ 0 for i in range ( 10 )]
dc = 0
while True :
digits[dc] = n % 10
n = n / / 10
dc + = 1
if (n = = 0 ):
break
for i in range (dc - 1 , - 1 , - 1 ):
print (word[digits[i]],end = " " )
n = 350
printWordsWithoutIfSwitch(n)
|
Javascript
<script>
let word=[ "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" ];
function printWordsWithoutIfSwitch(n)
{
let digits = new Array(10);
let dc = 0;
do
{
digits[dc] = n % 10;
n = Math.floor(n/10);
dc++;
} while (n != 0);
for (let i = dc - 1; i >= 0; i--)
document.write(word[digits[i]] + " " );
}
let n = 350;
printWordsWithoutIfSwitch(n);
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...