A positive integer is entered through the keyboard. Along with it the base of the numbering system in which you want to convert this number is entered. Write a program to display the number entered, the base, and the converted number. For example, if the input is 64 2 then the output should be 64 2 1000000. Similarly, if the input is 64 16, then the output should be 64 16 40.

Below is a Java program that takes a positive integer and a base as inputs, then converts the integer to the specified base and displays the original number, the base, and the converted number.

Number conversion based on input base

				
					import java.util.Scanner;

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

        // Input the number and the base
        System.out.print("Enter a positive integer: ");
        int number = scanner.nextInt();
        
        System.out.print("Enter the base to convert to: ");
        int base = scanner.nextInt();

        // Convert the number to the specified base
        String convertedNumber = Integer.toString(number, base).toUpperCase();

        // Display the results
        System.out.println(number + " " + base + " " + convertedNumber);
        
        scanner.close();
    }
}

				
			

How the Program Works:

  1. Input Handling: The program uses Scanner to read inputs from the user. It prompts for a positive integer and the base to convert to.
  2. Conversion: The program uses Integer.toString(int number, int base) method to convert the given number to the specified base. The toUpperCase() method ensures that the output is in uppercase for bases greater than 10 (e.g., hexadecimal).
  3. Output: It prints the original number, the base, and the converted number in the specified base.

Example Runs:

Example 1:

				
					Enter a positive integer: 64
Enter the base to convert to: 2
64 2 1000000

				
			

Example 2:

				
					Enter a positive integer: 64
Enter the base to convert to: 16
64 16 40

				
			

Explanation:

  • In the first example, 64 in base 2 is 1000000.
  • In the second example, 64 in base 16 (hexadecimal) is 40.

This program will correctly handle any positive integer and base within the typical range of bases used (2 to 36).

prime number

Another Example

Below is a Java program that takes a positive integer and a base as input, and then displays the number in the specified base.

				
					import java.util.Scanner;

public class NumberBaseConverter {

    public static String convertToBase(int number, int base) {
        if (number == 0) {
            return "0";
        }

        StringBuilder result = new StringBuilder();
        char[] digits = "0123456789ABCDEF".toCharArray();

        while (number > 0) {
            result.insert(0, digits[number % base]);
            number /= base;
        }

        return result.toString();
    }

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

        System.out.print("Enter a positive integer: ");
        int number = scanner.nextInt();

        if (number < 0) {
            System.out.println("Error: Only positive integers are allowed.");
            return;
        }

        System.out.print("Enter the base (between 2 and 16): ");
        int base = scanner.nextInt();

        if (base < 2 || base > 16) {
            System.out.println("Error: Base must be between 2 and 16.");
            return;
        }

        String convertedNumber = convertToBase(number, base);
        
        System.out.println(number + " " + base + " " + convertedNumber);
    }
}

				
			

Explanation:

  1. Input Handling:

    • The program uses the Scanner class to read user input.
    • It takes an integer number and a base from the user.
    • It checks if the number is positive and the base is within the valid range (2 to 16).
  2. Conversion Function:

    • The function convertToBase is responsible for converting the number to the desired base.
    • It uses a StringBuilder to build the converted number.
    • The conversion is done using repeated division and modulus operations to extract digits, which are prepended to the result.
    • The array digits maps integer values to their corresponding characters in bases up to 16.
  3. Output:

    • The program prints the original number, the base, and the converted number in the specified format.

Example Outputs:

  • If the input is 64 and 2, the output will be:

				
					Enter a positive integer: 64
Enter the base (between 2 and 16): 2
64 2 1000000

				
			

If the input is 64 and 16, the output will be:

				
					Enter a positive integer: 64
Enter the base (between 2 and 16): 16
64 16 40

				
			

This program ensures that the input is valid and then correctly converts and displays the number in the desired base.

Scroll to Top