Q:

Java program to print the value in decimal, octal, hexadecimal using printf() method

belongs to collection: Java Basic Programs

0

Java program to print the value in decimal, octal, hexadecimal using printf() method

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

// 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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to calculate the employee and employe... >>
<< Java program to perform subtraction without using ...