 Open in App
Not now

# Program to print the given digit in words

• Difficulty Level : Easy
• Last Updated : 20 Dec, 2022

Given a number N, the task is to convert every digit of the number into words.

Examples:

Input: N = 1234
Output: One Two Three Four
Explanation:
Every digit of the given number has been converted into its corresponding word.

Input: N = 567
Output: Five Six Seven

Approach: The idea is to traverse through every digit of the number and use switch-case. Since there are only ten possible values for digits, ten cases can be defined inside a switch block. For each digit, its corresponding case block will be executed and that digit will get printed in words.

Below is the implementation of the above approach:

## CPP

 `// C++ implementation of the above approach` `#include "bits/stdc++.h"``using` `namespace` `std;` `// Function to return the word``// of the corresponding digit``void` `printValue(``char` `digit)``{` `    ``// Switch block to check for each digit c``    ``switch` `(digit) {` `    ``// For digit 0``    ``case` `'0'``:``        ``cout << ``"Zero "``;``        ``break``;` `    ``// For digit 1``    ``case` `'1'``:``        ``cout << ``"One "``;``        ``break``;` `    ``// For digit 2``    ``case` `'2'``:``        ``cout << ``"Two "``;``        ``break``;` `    ``// For digit 3``    ``case` `'3'``:``        ``cout << ``"Three "``;``        ``break``;` `    ``// For digit 4``    ``case` `'4'``:``        ``cout << ``"Four "``;``        ``break``;` `    ``// For digit 5``    ``case` `'5'``:``        ``cout << ``"Five "``;``        ``break``;` `    ``// For digit 6``    ``case` `'6'``:``        ``cout << ``"Six "``;``        ``break``;` `    ``// For digit 7``    ``case` `'7'``:``        ``cout << ``"Seven "``;``        ``break``;` `    ``// For digit 8``    ``case` `'8'``:``        ``cout << ``"Eight "``;``        ``break``;` `    ``// For digit 9``    ``case` `'9'``:``        ``cout << ``"Nine "``;``        ``break``;``    ``}``}` `// Function to iterate through every``// digit in the given number``void` `printWord(string N)``{``    ``int` `i, length = N.length();` `    ``// Finding each digit of the number``    ``for` `(i = 0; i < length; i++) {` `        ``// Print the digit in words``        ``printValue(N[i]);``    ``}``}` `// Driver code``int` `main()``{``    ``string N = ``"123"``;``    ``printWord(N);``    ``return` `0;``}`

## Java

 `// Java implementation of the above approach``class` `GFG``{` `// Function to return the word``// of the corresponding digit``static` `void` `printValue(``char` `digit)``{` `    ``// Switch block to check for each digit c``    ``switch` `(digit)``    ``{` `    ``// For digit 0``    ``case` `'0'``:``        ``System.out.print(``"Zero "``);``        ``break``;` `    ``// For digit 1``    ``case` `'1'``:``        ``System.out.print(``"One "``);``        ``break``;` `    ``// For digit 2``    ``case` `'2'``:``        ``System.out.print(``"Two "``);``        ``break``;` `    ``// For digit 3``    ``case` `'3'``:``        ``System.out.print(``"Three "``);``        ``break``;` `    ``// For digit 4``    ``case` `'4'``:``        ``System.out.print(``"Four "``);``        ``break``;` `    ``// For digit 5``    ``case` `'5'``:``        ``System.out.print(``"Five "``);``        ``break``;` `    ``// For digit 6``    ``case` `'6'``:``        ``System.out.print(``"Six "``);``        ``break``;` `    ``// For digit 7``    ``case` `'7'``:``        ``System.out.print(``"Seven "``);``        ``break``;` `    ``// For digit 8``    ``case` `'8'``:``        ``System.out.print(``"Eight "``);``        ``break``;` `    ``// For digit 9``    ``case` `'9'``:``        ``System.out.print(``"Nine "``);``        ``break``;``    ``}``}` `// Function to iterate through every``// digit in the given number``static` `void` `printWord(String N)``{``    ``int` `i, length = N.length();` `    ``// Finding each digit of the number``    ``for` `(i = ``0``; i < length; i++)``    ``{` `        ``// Print the digit in words``        ``printValue(N.charAt(i));``    ``}``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``String N = ``"123"``;``    ``printWord(N);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 implementation of the above approach` `# Function to return the word``# of the corresponding digit``def` `printValue(digit):` `    ``# Switch block to check for each digit c` `    ``# For digit 0``    ``if` `digit ``=``=` `'0'``:``        ``print``(``"Zero "``, end ``=` `" "``)` `    ``# For digit 1``    ``elif` `digit ``=``=` `'1'``:``        ``print``(``"One "``, end ``=` `" "``)` `    ``# For digit 2``    ``elif` `digit ``=``=` `'2'``:``        ``print``(``"Two "``, end ``=` `" "``)` `    ``#For digit 3``    ``elif` `digit``=``=``'3'``:``        ``print``(``"Three"``,end``=``" "``)` `    ``# For digit 4``    ``elif` `digit ``=``=` `'4'``:``        ``print``(``"Four "``, end ``=` `" "``)` `    ``# For digit 5``    ``elif` `digit ``=``=` `'5'``:``        ``print``(``"Five "``, end ``=` `" "``)` `    ``# For digit 6``    ``elif` `digit ``=``=` `'6'``:``        ``print``(``"Six "``, end ``=` `" "``)` `    ``# For digit 7``    ``elif` `digit ``=``=` `'7'``:``        ``print``(``"Seven"``, end ``=` `" "``)` `    ``# For digit 8``    ``elif` `digit ``=``=` `'8'``:``        ``print``(``"Eight"``, end ``=` `" "``)` `    ``# For digit 9``    ``elif` `digit ``=``=` `'9'``:``        ``print``(``"Nine "``, end ``=` `" "``)` `# Function to iterate through every``# digit in the given number``def` `printWord(N):``    ``i ``=` `0``    ``length ``=` `len``(N)` `    ``# Finding each digit of the number``    ``while` `i < length:``        ` `        ``# Print the digit in words``        ``printValue(N[i])``        ``i ``+``=` `1` `# Driver code``N ``=` `"123"``printWord(N)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# implementation of the above approach``using` `System;` `class` `GFG``{` `    ``// Function to return the word``    ``// of the corresponding digit``    ``static` `void` `printValue(``char` `digit)``    ``{``    ` `        ``// Switch block to check for each digit c``        ``switch` `(digit)``        ``{``    ` `        ``// For digit 0``        ``case` `'0'``:``            ``Console.Write(``"Zero "``);``            ``break``;``    ` `        ``// For digit 1``        ``case` `'1'``:``            ``Console.Write(``"One "``);``            ``break``;``    ` `        ``// For digit 2``        ``case` `'2'``:``            ``Console.Write(``"Two "``);``            ``break``;``    ` `        ``// For digit 3``        ``case` `'3'``:``            ``Console.Write(``"Three "``);``            ``break``;``    ` `        ``// For digit 4``        ``case` `'4'``:``            ``Console.Write(``"Four "``);``            ``break``;``    ` `        ``// For digit 5``        ``case` `'5'``:``            ``Console.Write(``"Five "``);``            ``break``;``    ` `        ``// For digit 6``        ``case` `'6'``:``            ``Console.Write(``"Six "``);``            ``break``;``    ` `        ``// For digit 7``        ``case` `'7'``:``            ``Console.Write(``"Seven "``);``            ``break``;``    ` `        ``// For digit 8``        ``case` `'8'``:``            ``Console.Write(``"Eight "``);``            ``break``;``    ` `        ``// For digit 9``        ``case` `'9'``:``            ``Console.Write(``"Nine "``);``            ``break``;``        ``}``    ``}``    ` `    ``// Function to iterate through every``    ``// digit in the given number``    ``static` `void` `printWord(``string` `N)``    ``{``        ``int` `i, length = N.Length;``    ` `        ``// Finding each digit of the number``        ``for` `(i = 0; i < length; i++)``        ``{``    ` `            ``// Print the digit in words``            ``printValue(N[i]);``        ``}``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``string` `N = ``"123"``;``        ``printWord(N);``    ``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output

`One Two Three `

Time Complexity: O(L), Here L is the length of the string
Auxiliary Space: O(1), As constant extra space is used.

Recursive Approach

The idea is to recursively call the function till the number becomes zero by dividing it by 10 and storing the remainder in a variable. Then we print the digit from the string array as the digit will store the index of the number to be printed from the array.

we print the number after the recursive call to maintain the order of digits in the input number, if we print before the recursive function call the digit name will be printed in reversed order. Since we are dividing n by 10 in each recursive call the recurrence relation will be  T(n) = T(n/10) + 1

Below is the implementation of the above approach:

## C++

 `//C++ implementation of above approach``#include ``using` `namespace` `std;` `void` `ToDigits(``int` `n, string arr[])``{` `    ``// base case``    ``if` `(n == 0) {``        ``return``;``    ``}` `    ``// storing the last digit of the number and updating``    ``// number``    ``int` `digit = n % 10;``    ``n = n / 10;` `    ``// recursive call``    ``ToDigits(n, arr);` `    ``// printing the digits form the string array storing name``    ``// of the given index``    ``cout << arr[digit] << ``" "``;``}` `int` `main()``{` `    ``string arr``        ``= { ``"zero"``, ``"one"``, ``"two"``,   ``"three"``, ``"four"``,``            ``"five"``, ``"six"``, ``"seven"``, ``"eight"``, ``"nine"` `};``    ``int` `n;``    ``n = 123;  ``//it can be changed to take user input` `    ``ToDigits(n, arr);` `    ``return` `0;``}``//This code is contributed by rahulpatel43433`

## Java

 `/*package whatever //do not write package name here */``import` `java.io.*;` `class` `GFG {``  ``static` `void` `ToDigits(``int` `n, String[] arr)``  ``{` `    ``// base case``    ``if` `(n == ``0``) {``      ``return``;``    ``}` `    ``// storing the last digit of the number and updating``    ``// number``    ``int` `digit = n % ``10``;``    ``n = n / ``10``;` `    ``// recursive call``    ``ToDigits(n, arr);` `    ``// printing the digits form the string array storing name``    ``// of the given index``    ``System.out.print(arr[digit]);``    ``System.out.print(``" "``);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String args[])``  ``{``    ``String[] arr = { ``"zero"``, ``"one"``, ``"two"``,   ``"three"``, ``"four"``,    ``"five"``, ``"six"``, ``"seven"``, ``"eight"``, ``"nine"` `};``    ``int` `n = ``123``;  ``//it can be changed to take user input` `    ``ToDigits(n, arr);``  ``}``}` `// This code is contributed by shinjanpatra`

## Python3

 `# Python implementation of above approach``def` `ToDigits(n, arr):` `    ``# base case``    ``if` `(n ``=``=` `0``):``        ``return` `    ``# storing the last digit of the number and updating``    ``# number``    ``digit ``=` `n ``%` `10``    ``n ``=` `n ``/``/` `10` `    ``# recursive call``    ``ToDigits(n, arr)` `    ``# printing the digits form the string array storing name``    ``# of the given index``    ``print``(arr[digit] , end ``=` `" "``)`  `# driver code``arr ``=` `[ ``"zero"``, ``"one"``, ``"two"``, ``"three"``, ``"four"``,``            ``"five"``, ``"six"``, ``"seven"``, ``"eight"``, ``"nine"` `]``n ``=` `123` `#it can be changed to take user input` `ToDigits(n, arr)` `# This code is contributed by shinjanpatra`

## C#

 `//c# implementation of above approach``using` `System;`` ` `public` `class` `GFG {``    ` `    ``static` `void` `ToDigits(``int` `n, String[] arr)``    ``{``        ``// base case``        ``if` `(n == 0) {``            ``return``;``        ``}``        ` `        ``// storing the last digit of the number and updating``        ``// number``        ``int` `digit = n % 10;``        ``n = n / 10;``        ` `        ``// recursive call``        ``ToDigits(n, arr);``        ` `        ``// printing the digits form the string array storing name``        ``// of the given index``        ``Console.Write(arr[digit]+``" "``);``    ``}``     ` `    ``// Driver program to test above``    ``public` `static` `void` `Main()``    ``{``        ``String[] arr = ``new` `string``{ ``"zero"``, ``"one"``, ``"two"``,   ``"three"``, ``"four"``, ``"five"``,``        ``"six"``, ``"seven"``, ``"eight"``, ``"nine"` `};``        ` `        ``int` `n;``        ``n = 123;  ``//it can be changed to take user input``        ` `        ``ToDigits(n, arr);``    ``}``}``// This code is contributed by aditya942003patil`

## Javascript

 ``

Output

`one two three `

Time Complexity: O(log10 n)
Auxiliary Space: O(log10 n) for recursive call stack

Related Article: Print individual digits as words without using if or switch

My Personal Notes arrow_drop_up