Program to convert Number in characters

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.