Home »
Java Programs »
Java Basic Programs
Java program to print the value in decimal, octal, hexadecimal using printf() method
Input a value, write a Java program to print the value in decimal, octal, hexadecimal using printf() method.
Submitted by Nidhi, on February 23, 2022
Problem statement
In this program, we will read an integer number from the user and print its corresponding Decimal, Hexadecimal, Octal number using the printf() method.
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.
// 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.
Java Basic Programs »