Skip to content
Related Articles

Related Articles

Program to convert Number in characters
  • Difficulty Level : Basic
  • Last Updated : 10 Sep, 2020

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 << "UnValid ";
            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("UnValid ");
            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, "UnValid"),
               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("UnValid ");
                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
Output: 
one two three four five



 

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :