 Open in App
Not now

# Print individual digits as words without using if or switch

• Difficulty Level : Easy
• 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.

## C++

 `// C++ program to print individual words without if and``// without switch``#include ``using` `namespace` `std;` `// To store digit to word mapping``char` `word[] = {``"zero"``, ``"one"``, ``"two"``, ``"three"``,``"four"``,``                   ``"five"``, ``"six"``, ``"seven"``, ``"eight"``, ``"nine"``};` `void` `printWordsWithoutIfSwitch(``int` `n)``{``    ``// Store individual digits``    ``int` `digits; ``// 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;``}`

## Java

 `// Java program to print individual words without``//  if and without switch``import` `java.io.*;``public` `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`

## 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``; ``// 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`

## Python3

 `# Python program to print individual 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 print words using``    ``# word[][]``    ``for` `i ``in` `range``(dc``-``1``,``-``1``,``-``1``):``        ``print``(word[digits[i]],end``=``" "``)` `# Driver program``n ``=` `350``printWordsWithoutIfSwitch(n)` `# This code is contributed by mohit kumar 29`

## Javascript

 ``

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.