Write a Java Program to convert int type variables to char

Java Program to convert int type variables to char

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to convert int type variables to char
Java Program to convert int type variables to char

Write a Java Program to convert int type variables to char

PROGRAM: Java Program to convert int type variables to char

/*Java Program to convert int type variables to char*/

1: Java Program to Convert int to char

class Main {
public static void main(String[] args) {

// create int variables
int num1 = 80;
int num2 = 81;

// convert int to char
// typecasting
char a = (char)num1;
char b = (char)num2;

// print value
System.out.println(a); // P
System.out.println(b); // Q
}
}

2: int to char by using forDigit()

class Main {
public static void main(String[] args) {

// create int variables
int num1 = 1;
int num2 = 13;

// convert int to char
// for value between 0-9
char a = Character.forDigit(num1, 10);

// for value between 0-9
char b = Character.forDigit(num2, 16);

// print value
System.out.println(a); // 1
System.out.println(b); // d
}
}

3: int to char by adding ‘0’

class Main {
public static void main(String[] args) {

// create int variables
int num1 = 1;
int num2 = 9;

// convert int to char
char a = (char)(num1 + ‘0’);
char b = (char)(num2 + ‘0’);

// print value
System.out.println(a); // 1
System.out.println(b); // 9
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *