Program to print the given digit in words
Last Updated :
28 Mar, 2023
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
#include "bits/stdc++.h"
using namespace std;
void printValue( char digit)
{
switch (digit) {
case '0' :
cout << "Zero " ;
break ;
case '1' :
cout << "One " ;
break ;
case '2' :
cout << "Two " ;
break ;
case '3' :
cout << "Three " ;
break ;
case '4' :
cout << "Four " ;
break ;
case '5' :
cout << "Five " ;
break ;
case '6' :
cout << "Six " ;
break ;
case '7' :
cout << "Seven " ;
break ;
case '8' :
cout << "Eight " ;
break ;
case '9' :
cout << "Nine " ;
break ;
}
}
void printWord(string N)
{
int i, length = N.length();
for (i = 0; i < length; i++) {
printValue(N[i]);
}
}
int main()
{
string N = "123" ;
printWord(N);
return 0;
}
|
Java
class GFG
{
static void printValue( char digit)
{
switch (digit)
{
case '0' :
System.out.print( "Zero " );
break ;
case '1' :
System.out.print( "One " );
break ;
case '2' :
System.out.print( "Two " );
break ;
case '3' :
System.out.print( "Three " );
break ;
case '4' :
System.out.print( "Four " );
break ;
case '5' :
System.out.print( "Five " );
break ;
case '6' :
System.out.print( "Six " );
break ;
case '7' :
System.out.print( "Seven " );
break ;
case '8' :
System.out.print( "Eight " );
break ;
case '9' :
System.out.print( "Nine " );
break ;
}
}
static void printWord(String N)
{
int i, length = N.length();
for (i = 0 ; i < length; i++)
{
printValue(N.charAt(i));
}
}
public static void main(String[] args)
{
String N = "123" ;
printWord(N);
}
}
|
Python3
def printValue(digit):
if digit = = '0' :
print ( "Zero " , end = " " )
elif digit = = '1' :
print ( "One " , end = " " )
elif digit = = '2' :
print ( "Two " , end = " " )
elif digit = = '3' :
print ( "Three" ,end = " " )
elif digit = = '4' :
print ( "Four " , end = " " )
elif digit = = '5' :
print ( "Five " , end = " " )
elif digit = = '6' :
print ( "Six " , end = " " )
elif digit = = '7' :
print ( "Seven" , end = " " )
elif digit = = '8' :
print ( "Eight" , end = " " )
elif digit = = '9' :
print ( "Nine " , end = " " )
def printWord(N):
i = 0
length = len (N)
while i < length:
printValue(N[i])
i + = 1
N = "123"
printWord(N)
|
C#
using System;
class GFG
{
static void printValue( char digit)
{
switch (digit)
{
case '0' :
Console.Write( "Zero " );
break ;
case '1' :
Console.Write( "One " );
break ;
case '2' :
Console.Write( "Two " );
break ;
case '3' :
Console.Write( "Three " );
break ;
case '4' :
Console.Write( "Four " );
break ;
case '5' :
Console.Write( "Five " );
break ;
case '6' :
Console.Write( "Six " );
break ;
case '7' :
Console.Write( "Seven " );
break ;
case '8' :
Console.Write( "Eight " );
break ;
case '9' :
Console.Write( "Nine " );
break ;
}
}
static void printWord( string N)
{
int i, length = N.Length;
for (i = 0; i < length; i++)
{
printValue(N[i]);
}
}
public static void Main()
{
string N = "123" ;
printWord(N);
}
}
|
Javascript
<script>
function printValue(digit) {
switch (digit) {
case "0" :
document.write( "Zero " );
break ;
case "1" :
document.write( "One " );
break ;
case "2" :
document.write( "Two " );
break ;
case "3" :
document.write( "Three " );
break ;
case "4" :
document.write( "Four " );
break ;
case "5" :
document.write( "Five " );
break ;
case "6" :
document.write( "Six " );
break ;
case "7" :
document.write( "Seven " );
break ;
case "8" :
document.write( "Eight " );
break ;
case "9" :
document.write( "Nine " );
break ;
}
}
function printWord(N) {
var i,
length = N.length;
for (i = 0; i < length; i++) {
printValue(N[i]);
}
}
var N = "123" ;
printWord(N);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
void ToDigits( int n, string arr[])
{
if (n == 0) {
return ;
}
int digit = n % 10;
n = n / 10;
ToDigits(n, arr);
cout << arr[digit] << " " ;
}
int main()
{
string arr[10]
= { "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" };
int n;
n = 123;
ToDigits(n, arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void ToDigits( int n, String[] arr)
{
if (n == 0 ) {
return ;
}
int digit = n % 10 ;
n = n / 10 ;
ToDigits(n, arr);
System.out.print(arr[digit]);
System.out.print( " " );
}
public static void main(String args[])
{
String[] arr = { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" };
int n = 123 ;
ToDigits(n, arr);
}
}
|
Python3
def ToDigits(n, arr):
if (n = = 0 ):
return
digit = n % 10
n = n / / 10
ToDigits(n, arr)
print (arr[digit] , end = " " )
arr = [ "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" ]
n = 123
ToDigits(n, arr)
|
C#
using System;
public class GFG {
static void ToDigits( int n, String[] arr)
{
if (n == 0) {
return ;
}
int digit = n % 10;
n = n / 10;
ToDigits(n, arr);
Console.Write(arr[digit]+ " " );
}
public static void Main()
{
String[] arr = new string [10]{ "zero" , "one" , "two" , "three" , "four" , "five" ,
"six" , "seven" , "eight" , "nine" };
int n;
n = 123;
ToDigits(n, arr);
}
}
|
Javascript
<script>
function ToDigits(n, arr)
{
if (n == 0) {
return ;
}
let digit = n % 10;
n = Math.floor(n / 10);
ToDigits(n, arr);
document.write(arr[digit] , " " );
}
let arr = [ "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" ]
let n = 123;
ToDigits(n, arr);
</script>
|
Time Complexity: O(log10 n)
Auxiliary Space: O(log10 n) for recursive call stack
Approach 3 : By using Python Dictionary.
Algorithm –
- Create a dictionary named digits, assign key-value pairs of numbers to words respectively, as shown in code below.
- Iterate the string character one by one and print the value of the dictionary, key associated with it.
C++
#include <iostream>
#include <string>
#include <map>
using namespace std;
void printWord(string N) {
map< char , string> digits {
{ '1' , "One" }, { '2' , "Two" }, { '3' , "Three" }, { '4' , "Four" },
{ '5' , "Five" }, { '6' , "Six" }, { '7' , "Seven" }, { '8' , "Eight" },
{ '9' , "Nine" }, { '0' , "Zero" }
};
for ( char number : N) {
cout << digits[number] << " " ;
}
}
int main() {
string N = "123" ;
printWord(N);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void printWord(String N) {
Map<Character, String> digits = new HashMap<Character, String>() {{
put( '1' , "One" );
put( '2' , "Two" );
put( '3' , "Three" );
put( '4' , "Four" );
put( '5' , "Five" );
put( '6' , "Six" );
put( '7' , "Seven" );
put( '8' , "Eight" );
put( '9' , "Nine" );
put( '0' , "Zero" );
}};
for ( char number : N.toCharArray()) {
System.out.print(digits.get(number) + " " );
}
}
public static void main(String[] args) {
String N = "123" ;
printWord(N);
}
}
|
Python3
def printWord(N):
digits = { '1' : 'One' , '2' : 'Two' , '3' : 'Three' , '4' : 'Four' , '5' : 'Five' , '6' : 'Six' , '7' : 'Seven' , '8' : 'Eight' , '9' : 'Nine' , '0' : 'Zero' }
for number in N:
print (digits[number] , end = ' ' )
N = "123"
printWord(N)
|
C#
using System;
using System.Collections.Generic;
class Program {
static void Main( string [] args)
{
string N = "123" ;
PrintWord(N);
}
static void PrintWord( string N)
{
Dictionary< char , string > digits
= new Dictionary< char , string >{
{ '1' , "One" }, { '2' , "Two" },
{ '3' , "Three" }, { '4' , "Four" },
{ '5' , "Five" }, { '6' , "Six" },
{ '7' , "Seven" }, { '8' , "Eight" },
{ '9' , "Nine" }, { '0' , "Zero" }
};
foreach ( char number in N)
{
Console.Write(digits[number] + " " );
}
}
}
|
Javascript
function printWord(N) {
const digits = { '1' : 'One' , '2' : 'Two' , '3' : 'Three' , '4' : 'Four' , '5' : 'Five' , '6' : 'Six' , '7' : 'Seven' , '8' : 'Eight' , '9' : 'Nine' , '0' : 'Zero' };
for (let number of N) {
console.log(digits[number] + ' ' );
}
}
const N = "123" ;
printWord(N);
|
Time Complexity: O(n), where n is the length of string
Auxiliary Space: O(1)
Related Article: Print individual digits as words without using if or switch
Share your thoughts in the comments
Please Login to comment...