The source code to print the value in Decimal, Octal, Hexadecimal using printf() method is given below. The given program is compiled and executed successfully.
// Java program to print the value in Decimal,
// Octal, Hexadecimal using printf() method
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num = 0;
Scanner myObj = new Scanner(System.in);
System.out.printf("Enter number: ");
num = myObj.nextInt();
System.out.printf("Decimal number: %d\n", num);
System.out.printf("Hexadecimal number: %X\n", num);
System.out.printf("Octal number: %o\n", num);
}
}
Output:
Enter number: 14
Decimal number: 14
Hexadecimal number: E
Octal number: 16
Explanation:
In the above program, we imported the "java.util.Scanner" package to read input from the user. And, we created a public class Main. It contains a static method main().
The main() method is an entry point for the program. Here, we read an integer number from the user using the nextInt() method and printed the corresponding decimal, hexadecimal, and octal number.
Program/Source Code:
The source code to print the value in Decimal, Octal, Hexadecimal using printf() method is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported the "java.util.Scanner" package to read input from the user. And, we created a public class Main. It contains a static method main().
The main() method is an entry point for the program. Here, we read an integer number from the user using the nextInt() method and printed the corresponding decimal, hexadecimal, and octal number.
need an explanation for this answer? contact us directly to get an explanation for this answer