Java Program For Int to Char Conversion
As we are aware char in Java takes 1 byte while int takes 4 bytes. So if we want integer getting converted to character than we need to typecast because data residing in 4 bytes can not get into a single byte. So prior moving ahead readers must have to be well verse with typecasting in java.
Tip: It is also recommended to go through difference between type conversion and type casting in java.
There are many operations which can be computed over strings but not over characters so we generally see in Java that many operations are computed. Here the input provided to us is an integer value say it be ‘N’ and the task is to convert the number in characters.
Illustrations:
Input : N = 74254 Output : Seven four two five four
Input : N = 23 Output : Two three
Methods:
- Using switch case (naive Approach)
- Using concept of typecasting (Naive Apporach)
- Using modulo operator(Optimal approach)
Let us discuss each of above listed methods via laying their corresponding approach and implementing with help of clean java codes.
Method 1: Using switch case
Approach:
- Convert a number into a string.
- Start traversing the string and print the equivalent character string for the number.
Example:
Java
// Java Program to Convert Integer to 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 switch case using length() method for ( int i = 0 ; i < s.length(); i++) { // Switch case // Reading digits one by one // using charAt() method switch (s.charAt(i)) { // Case 1 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 ; // Case 10 case '0' : System.out.print( "zero " ); break ; // Default case default : System.out.print( "InValid " ); break ; } } } // Method 2 // Main driver method public static void main(String[] args) { // Custom input Integer int n = 12345 ; // Calling the above function NumbertoCharacter(n + "" ); } } |
one two three four five
Method 2: Using concept of Type-casting
Here we will simply explicitly type cast the datatype and will store into character. As we all know there is ASCII table that holds a specific value corresponding to 256 symbols of english language. So automatically it will store the corresponding symbol corresponding to digit.
Example:
Java
// Java Program to Illustrate Integer to // Character Conversion // Using Concept of Type-casting // Importing required classes import java.util.*; // Class class GFG { // Main driver method public static void main(String[] args) { // Custom integer input int i = 97 ; // Type casting character to integer char ch = ( char )i; // Print statement System.out.println(ch); } } |
a
Similarly we can do the same via adding zero to integer value and later on we will do same as we did in above example that is we will typecast the above same.
Example:
Java
// Java Program to Convert Integer to Character // of Integer Class Via Type-casting With Adding Zero import java.io.*; import java.lang.*; import java.util.*; class GFG { public static void main(String[] args) { int i = 64 ; char ch = ( char )(i + '0' ); System.out.println(ch); } } |
p
Method 3: Using Modulo Operator
Procedure:
- Reverse the number.
- Iterate through the reversed number from right to left.
- Extract the last digit by using modulus, then use a switch case to get the corresponding word.
- While iterating divide the number by 10.
Example:
Java
// Java program to Convert Number in Characters // Using Modulo Operator // 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( "InValid " ); 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 function(Method1) NumbertoCharacter(n); } } |
one two three four five
Time complexity: O(k), k is the length of the number.
Space complexity: O(1)