Write a program to check number in datatype?
Here we are writing a program to check number in datatype and passing this number inside if condition that return a datatype like byte,short,int.
byte range= -128 to 127
short range= -32768 to 32767
int range= -2147483648 to 2147483647
long range= -9223372036854775808L to 9223372036854775807L
import java.util.Scanner;
public class TestData {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
long m = sc.nextLong();
for (int i = 0; i <= m; i++) {
System.out.println(" enter value :"+m);
if (m >= -128 && m <= 127) {
System.out.println("byte");
}
if (m >= -32768 && m <= 32767) {
System.out.println("short");
}
if (m >= -2147483648 && m <= 2147483647) {
System.out.println("int");
}
if (m >= -9223372036854775808L && m <= 9223372036854775807L) {
System.out.println("long");
}
}
} catch (Exception e) {
System.out.println(sc.next() + " it is wrong value");
}
}
}
Input :- enter value :2147483647
Output :-
int
long
Write a program to check number in datatype? Read More »