Question

In: Computer Science

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent.

If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program.

The output of your program will always be a 7-digit octal number with no spaces between any of the digits. Some of the leading digits may be 0.

Use a while loop to solve the problem. Do not use strings.

Sample Program Run
Please enter a number between 0 and 2097151 to convert: 160000
Your integer number 160000 is 0470400 in octal.

Please enter a number between 0 and 2097151 to convert: 5000000
UNABLE TO CONVERT

Solutions

Expert Solution

import java.util.Scanner;

public class DecimalToOctal {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.print("Please enter a number between 0 and 2097151 to convert: ");
            int n = in.nextInt();
            if (n < 0 || n > 2097151) {
                System.out.println("UNABLE TO CONVERT");
                break;
            }
            int num = n;
            String total = "";
            while (n > 0) {
                total = n % 8 + total;
                n /= 8;
            }
            System.out.printf("Your integer number %d is %" + (7-total.length()) + "d%s in octal.\n", num, 0, total);
        }
    }
}


Related Solutions

Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers....
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers. The program should be run from the command prompt, output a text prompt to the screen, and then wait for the user to type in a number followed by the Enter key. (The legitimate range of user input values is any signed integer that can be represented in 32 bits.) After each number is entered, the program should determine and display the following information...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You should generate B complement signal (flip all the bits of Input B,
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT