I\'m in java 1 and i need help. can you help me write this code. I really will a
ID: 3666355 • Letter: I
Question
I'm in java 1 and i need help. can you help me write this code. I really will appreciate your help.thank you.
For this program, you are going to convert decimal (integer) numbers into their octal number equivalents You can read about decimal-to-octal number conversions at the following Wikipedia entry. The most relevant section will be section 2.1, "Decimal to Octal conversion". http://en.wikipedia.org/wiki/Octal 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 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 new lines in your output between each digit Here are a few examples of decimal numbers and their octal number equivalents: Decimal Number (the input from the user Octal Equivalent (what vour program would output 0000001 0000002 0000003 0000077 0000100 0000173 0001750 0023420 0047040 0077776 2 63 64 123 1000 10000 20000 32766 32767 160000 262143 262144 2097151 0470400 1000000 Goals Learn about the octal number system . Use arithmetic to calculate and output the correct equivalent of the entered decimal number. . Use a repetition structure to solve a repetitious task.Explanation / Answer
import java.util.Scanner;
public class DecimalToOctal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number betweeb 0 and 2097151 to convert: ");
int decimal = sc.nextInt();
if(decimal > 2097151)
System.out.println("UNABLE TO CONVER - NUMBER OUT OF RANGE");
int count = 1;
String octal="";
int copyDecimal = decimal;
while(decimal > 0){
octal = (decimal%8) +octal;
decimal/=8;
count++;
}
while(count < 8){
octal = 0 + octal;
count++;
}
System.out.println("Your decimal integer number "+copyDecimal+" is "+octal+" in octal");
}
}
/*
OUTPUT:
Please enter a number betweeb 0 and 2097151 to convert:
160000
Your decimal integer number 160000 is 0470400 in octal
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.