Java Program For Int to Char Conversion
Given an Integer N. The task is to convert the number in characters.
Illustration:
Input : N = 74254 Output: Seven four two five four Input : N = 23 Output: Two three
Methods:
- Without using modulo operator(Naive approach)
- Using modulo operator(optimal approach)
Approach:
- Convert a number into a string.
- Start traversing the string and print the equivalent character string for the number.
Implementation:
Example 1
Java
// Java program to Convert Number in Characters // Importing input output classes import java.io.*; // Main class public class GFG { // Method 1 // To convert number to a character static void NumbertoCharacter(String s) { // Iterating the number taking one digit at a time // via swich case using length() method for ( int i = 0 ; i < s.length(); i++) { // Swich case // Reading digits obe by one // using charAtt() method switch (s.charAt(i)) { // Case1 case '1' : System.out.print( "one " ); // Break statement to hault // normal execution of the program break ; // Case 2 case '2' : System.out.print( "two " ); break ; // Case 3 case '3' : System.out.print( "three " ); break ; // Case 4 case '4' : System.out.print( "four " ); break ; // Case 5 case '5' : System.out.print( "five " ); break ; // Case 6 case '6' : System.out.print( "six " ); break ; // Case 7 case '7' : System.out.print( "seven " ); break ; // Case 8 case '8' : System.out.print( "eight " ); break ; // Case 9 case '9' : System.out.print( "nine " ); break ; // Default case case '0' : System.out.print( "zero " ); break ; default : System.out.print( "UnValid " ); break ; } } } // Method 2 // Main driver method public static void main(String[] args) { // Custom input Integer int n = 12345 ; // Calling the above function NumbertoCharacter(n + "" ); } } |
Output
one two three four five
Method 2: Using modulo operator(optimal approach)
Procedure:
- 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.
Implementation:
Example
Java
// Java program to Convert Number in Characters // Importing input output classes import java.io.*; // Main class public class GFG { // Method 1 // To convert numbers to characters static void NumbertoCharacter( int n) { // Initially declaring and initializing // reverse of number and remainder to zero int rev = 0 , r = 0 ; // If number is positive while (n > 0 ) { // For reversal of number // 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 case when above switch cases holds false default : System.out.print( "UnValid " ); break ; } // Divide the number by 10 // to get the next number rev = rev / 10 ; } } // Method 2 // Main driver method public static void main(String[] args) { // Custom input integer int n = 12345 ; // Calling the above fuction(Method1) NumbertoCharacter(n); } } |
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 Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.