Java Program to convert integer to character


March 15, 2022, Learn eTutorial
1031

Here we are explaining how to write a java program to convert an integer number to a character.

How to convert an integer number to a character in java?

In java, an integer number can be converted to a character by using typecasting. An example is shown below.

char ch= (char)num;  

Here num is the integer number which is converted into the character ch.

How to implement the java program to convert an integer number to a character using type casting?

First, we have to declare the class IntToChar.Create an object of the scanner class as sc. Read the integer number from the user into the variable n using sc.nextInt(). Then change the number n to a character as char c=(char)n. Then display the character c using System.out.println.

 

ALGORITHM

STEP 1: Declare the class IntToChar with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Create an object of the scanner class as sc.

STEP 4: Read the integer number from the user using sc.nextInt() into the variable n.

STEP 5: Change the integer number to a character as char c=(char)n.

STEP 6: Display the character c.

 

Java Source Code

                                          import java.util.Scanner;
public class IntToChar{  
   public static void main(String args[]){  
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter any integer Number");
 int n=sc.nextInt(); 
  
 //type casting
 char c= (char)n;  
 System.out.println(c);  
   }
}
                                      

OUTPUT

Enter any integer Number
80
P