Q. Write a program to receive an integer and find its octal, hexadecimal and binary equivalent.

Below is a Java program that receives an integer from the user and prints its octal, hexadecimal, and binary equivalents.

				
					import java.util.Scanner;

public class NumberConversion {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an integer: ");
        int num = scanner.nextInt();

        // Print octal equivalent
        String octal = Integer.toOctalString(num);
        System.out.println("Octal equivalent: " + octal);

        // Print hexadecimal equivalent
        String hexadecimal = Integer.toHexString(num).toUpperCase();
        System.out.println("Hexadecimal equivalent: " + hexadecimal);

        // Print binary equivalent
        String binary = Integer.toBinaryString(num);
        System.out.println("Binary equivalent: " + binary);

        scanner.close();
    }
}

				
			

Explanation:

  1. Import Statement:

    • import java.util.Scanner; is used to include the Scanner class for user input.
  2. Main Method:

    • A Scanner object is created to read input from the user.
    • The user is prompted to enter an integer, which is stored in the variable num.
    • The Integer.toOctalString(int i) method converts the integer to its octal string representation.
    • The Integer.toHexString(int i) method converts the integer to its hexadecimal string representation. The result is converted to uppercase for consistent formatting.
    • The Integer.toBinaryString(int i) method converts the integer to its binary string representation.
    • The results are printed to the console.

Sample Output:

				
					Enter an integer: 42
Octal equivalent: 52
Hexadecimal equivalent: 2A
Binary equivalent: 101010

				
			

To run this program, save it in a file named NumberConversion.java, compile it using javac NumberConversion.java, and run it using java NumberConversion. The program will prompt you to enter an integer and then display its octal, hexadecimal, and binary equivalents.

prime number

Simple program

				
					public class NumberConversion {
    public static void main(String[] args) {
        // Check if an integer is provided as command-line argument
        if (args.length == 0) {
            System.out.println("Usage: java NumberConversion <integer>");
            return;
        }

        // Parse the integer from command-line argument
        int num = Integer.parseInt(args[0]);

        // Print octal equivalent
        String octal = Integer.toOctalString(num);
        System.out.println("Octal equivalent: " + octal);

        // Print hexadecimal equivalent
        String hexadecimal = Integer.toHexString(num).toUpperCase();
        System.out.println("Hexadecimal equivalent: " + hexadecimal);

        // Print binary equivalent
        String binary = Integer.toBinaryString(num);
        System.out.println("Binary equivalent: " + binary);
    }
}

				
			

Explanation:

  1. Main Method:
    • The program checks if at least one command-line argument is provided.
    • If no argument is provided, it prints a usage message and exits.
    • Otherwise, it parses the first command-line argument as an integer using Integer.parseInt().
    • Then, it converts the integer to its octal, hexadecimal, and binary representations using Integer.toOctalString(), Integer.toHexString(), and Integer.toBinaryString() respectively.
    • Finally, it prints the results to the console.

Sample Output:

				
					$ java NumberConversion 255
Octal equivalent: 377
Hexadecimal equivalent: FF
Binary equivalent: 11111111

				
			

This program takes an integer as a command-line argument and then displays its octal, hexadecimal, and binary equivalents.

Scroll to Top