Skip to content
Related Articles

Related Articles

Program to convert Number in characters

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 08 Dec, 2021
Improve Article
Save Article

Given an Integer N. The task is to convert the number in characters.

Examples: 

Input: N = 74254 
Output: Seven four two five four

Input: N = 23 
Output: Two three  

An efficient approach:  

  1. Reverse the number.
  2. Iterate through the reversed number from right to left.
  3. Extract the last digit by using modulus, then use switch case to get the corresponding word.
  4. While iterating divide the number by 10.

C++




// C++ program to convert number in characters
#include<bits/stdc++.h>
using namespace std;
void NumbertoCharacter(int n)
{
    int rev = 0, r = 0;
 
    // To calculate the reverse of the number
    while (n > 0) {
 
        // The remainder will give the last digit of the number
        r = n % 10;
        rev = rev * 10 + r;
        n = n / 10;
    }
 
    while (rev > 0) {
        // Extract the first digit of the reversed number
        r = rev % 10;
 
        // Match it with switch case
        switch (r) {
        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;
        case 0:
            cout << "zero ";
            break;
        default:
            cout << "inValid ";
            break;
        }
 
        // Divide the number by 10 to get the next number
        rev = rev / 10;
    }
}
// Driver code
#include <iostream>
int main()
{
    int n = 12345;
    NumbertoCharacter(n);
    return 0;
}

Java




// Java program to convert number in characters
class GFG{
     
static void NumbertoCharacter(int n)
{
    int rev = 0, r = 0;
 
    // To calculate the reverse of the number
    while (n > 0)
    {
 
        // The remainder will give
        // the last digit of the number
        r = n % 10;
        rev = rev * 10 + r;
        n = n / 10;
    }
     
    while (rev > 0)
    {
 
        // Extract the first digit
        // of the reversed number
        r = rev % 10;
 
        // Match it with switch case
        switch (r)
        {
        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;
        case 0:
            System.out.print("zero ");
            break;
        default:
            System.out.print("InValid ");
            break;
        }
 
        // Divide the number by 10
        // to get the next number
        rev = rev / 10;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 12345;
    NumbertoCharacter(n);
}
}
 
// This code is contributed by Amit Katiyar

Python3




# Python3 program to convert
# number in characters
def NumbertoCharacter(n):
    rev = 0; r = 0;
 
    # To calculate the
    # reverse of the number
    while (n > 0):
 
        # The remainder will give
        # the last digit of the number
        r = n % 10;
        rev = rev * 10 + r;
        n = n // 10;   
 
    while (rev > 0):
 
        # Extract the first digit
        # of the reversed number
        r = rev % 10;
 
        # Match it with switch case
        switcher = {
                0 : "zero ",
                1 : "one ",
                2 : "two ",
                3 : "three ",
                4 : "four ",
                5 : "five ",
                6 : "six ",
                7 : "seven ",
                8 : "eight ",
                9 : "nine "
        }
        print( switcher.get(r, "InValid"),
               end = " ") ;       
 
        # Divide the number by 10
        # to get the next number
        rev = rev // 10;
 
# Driver code
if __name__ == '__main__':
    n = 12345;
    NumbertoCharacter(n);
 
# This code is contributed by gauravrajput1

C#




// C# program to convert number in characters
using System;
 
class GFG{
     
static void NumbertoCharacter(int n)
{
    int rev = 0, r = 0;
 
    // To calculate the reverse
    // of the number
    while (n > 0)
    {
         
        // The remainder will give
        // the last digit of the number
        r = n % 10;
        rev = rev * 10 + r;
        n = n / 10;
    }
     
    while (rev > 0)
    {
 
        // Extract the first digit
        // of the reversed number
        r = rev % 10;
 
        // Match it with switch case
        switch (r)
        {
            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;
            case 0:
                Console.Write("zero ");
                break;
            default:
                Console.Write("inValid ");
                break;
        }
 
        // Divide the number by 10
        // to get the next number
        rev = rev / 10;
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 12345;
     
    NumbertoCharacter(n);
}
}
 
// This code is contributed by sapnasingh4991

Javascript




<script>
 
// JavaScript program to convert number in characters
 
function NumbertoCharacter(n)
{
    let rev = 0, r = 0;
  
    // To calculate the reverse of the number
    while (n > 0)
    {
  
        // The remainder will give
        // the last digit of the number
        r = n % 10;
        rev = rev * 10 + r;
        n = Math.floor(n / 10);
    }
      
    while (rev > 0)
    {
  
        // Extract the first digit
        // of the reversed number
        r = rev % 10;
  
        // Match it with switch case
        switch (r)
        {
        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;
        case 0:
            document.write("zero ");
            break;
        default:
            document.write("UnValid ");
            break;
        }
  
        // Divide the number by 10
        // to get the next number
        rev = Math.floor(rev / 10);
    }
}
 
// Driver code
let n = 12345;
NumbertoCharacter(n);
 
 
// This code is contributed by patel2127
 
</script>

Output: 

one two three four five

 

Time complexity: O(k) 
k is the length of the number. 
Space complexity: O(1) 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!