Program to convert Number in characters - GeeksforGeeks

08 Dec, 2021
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:  
Reverse the number.Iterate through the reversed number from right to left.Extract the last digit by using modulus, then use switch case to get the corresponding word.While iterating divide the number by 10.
Reverse the number.
Iterate through the reversed number from right to left.
Extract the last digit by using modulus, then use switch case to get the corresponding word.
While iterating divide the number by 10.
C++
Java
Python3
C#
Javascript
// 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 program to convert number in charactersclass 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 codepublic static void main(String[] args){    int n = 12345;    NumbertoCharacter(n);}} // This code is contributed by Amit Katiyar
# Python3 program to convert# number in charactersdef 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 codeif __name__ == '__main__':    n = 12345;    NumbertoCharacter(n); # This code is contributed by gauravrajput1
// C# program to convert number in charactersusing 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 codepublic static void Main(String[] args){    int n = 12345;         NumbertoCharacter(n);}} // This code is contributed by sapnasingh4991
<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 codelet n = 12345;NumbertoCharacter(n);  // This code is contributed by patel2127 </script>
one two three four five
 
Time complexity: O(k) k is the length of the number. Space complexity: O(1) 
amit143katiyar
sapnasingh4991
GauravRajput1
patel2127
sagar0719kumar
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Program for factorial of a number
Program for Decimal to Binary Conversion
The Knight's tour problem | Backtracking-1
Operators in C / C++