Question

Description For this program, you are going to convert decimal (integer) numbers into their octal number...

Description

For this program, you are going to convert decimal (integer) numbers into their octal number (integer) equivalents. Make sure that you create a new Project and Java class file for this assignment. Your Repl.It file should be named “Main.java”. You can read about octal-to-decimal number conversions from wikepedia or another website

Instructions

  • 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 maybe 0. It may help to think that you may output each digit of the octal number individually as you calculate it, as long as you don't put spaces or newlines in your output between each digit.

  • Use a while loop to solve the problem.
  • DO NOT USE Strings.
  • Here are a few examples of decimal numbers and their octal number equivalents:
    octal conversion examples.png

Goals

  • Experience with the while loop.
  • Experience with if/else.

Sample Runs

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


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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Main.java : Java program to convert decimal number to octal
public class Main {

   public static void main(String[] args) {
      
       int inputNum, convertedNum;
       Scanner scan = new Scanner(System.in);
       // Input of integer number in decimal
       System.out.print("Please enter a number between 0 and 2097151 to convert: ");
       inputNum = scan.nextInt();
       // validate the number
       if(inputNum < 0 || inputNum > 2097151)
           System.out.println("UNABLE TO CONVERT");
       else
       {
           int weight=0; // weight of the digit, used for converting to octal
           convertedNum = 0; // variable to store the number in octal
           int temp = inputNum;
           int digit;
           // loop that continues till we convert the entire number to octal
           while(temp > 0)
           {
               digit = (temp%8); // get the remainder when the number is divided by 8
               // multiply the digit with its weight and add it to convertedNum
               convertedNum += digit*Math.pow(10, weight);
               temp = temp/8; // remove the last digit from the number
               weight++; // increment the weight
           }
           // display the integer in octal
           System.out.printf("Your integer number 160000 is %07d in octal",convertedNum);
       }
      
       scan.close();

}
}
//end of Main.java

Output:

Add a comment
Know the answer?
Add Answer to:
Description For this program, you are going to convert decimal (integer) numbers into their octal number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

  • 1. Convert the binary number 10101102 to octal, decimal, and hexadecimal numbers. 2. Convert the decimal...

    1. Convert the binary number 10101102 to octal, decimal, and hexadecimal numbers. 2. Convert the decimal number 236.7510 to binary,octal, and hexadecimal numbers. 3. Add the following two binary numbers: 100111102 and 011110112. Remember to show any carries that are generated along the way. 4. Repeat the previous question, but this time subtract the second binary number from the first. Remember to show any borrows that are required along the way. 5. Determine the encoding of the decimal number 28610...

  • Octal numbers have a base of eight and the digits 0–7. Write the scripts octalToDecimal.py and...

    Octal numbers have a base of eight and the digits 0–7. Write the scripts octalToDecimal.py and decimalToOctal.py, which convert numbers between the octal and decimal representations of integers. These scripts use algorithms that are similar to those of the binaryToDecimal and decimalToBinary scripts developed in the Section: Strings and Number Systems. An example of octalToDecimal.py input and output is shown below: Enter a string of octal digits: 234 The integer value is 156 An example of decimalToOctal.py input and output...

  • Question (15 pts.) Convert the decimal numbers from 0 to 32 to BCD, Hexadecimal, Octal, and...

    Question (15 pts.) Convert the decimal numbers from 0 to 32 to BCD, Hexadecimal, Octal, and Binary, using the minimum number of bits or digits, and without using calculators Fill your results in the table below. BCD Hex Oct |

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • a) The following program is supposed to convert a binary number to its decimal equivalent. Modify...

    a) The following program is supposed to convert a binary number to its decimal equivalent. Modify the program so that it does the job.(15 points) b) How would you create the executable file of the program (file name: b2d.c) in Linux? (5 points) #include <stdio.h> int main (void) int binary: printf("Enter a binary number:\n"); scanf("%d", &binary): int nofDigit = 0, remain = binary: while (remain > 0) - remain = remain/10; nofDigit++; int digit, dval = 0, bx = binary:...

  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT