ADVERTISEMENT
ADVERTISEMENT

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 Solution:

In this program, we will read an integer number from the user and print its corresponding Decimal, Hexadecimal, Octal number using the printf() method.

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.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.