In: Computer Science
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 user enters a number greater than 8 digits or a value less than 0, it should re-prompt the user to enter a number again*. You do not need to check if the digits are valid octal numbers (0-7), as this is guaranteed.
Instructions
main(){
int oct = user input;
conversion(oct);
}
void conversion(int o){
// logic goes here.
print(decimal);
}
Goals
Sample Runs
Sample Program Run (user input is underlined)
Enter up to an 8-digit octal number and I will convert it for you: 77777777
16777215
Sample Program Run (user input is underlined)
Enter up to an 8-digit octal number and I will convert it for you: 775002
260610
Sample Program Run (user input is underlined)
Enter up to an 8-digit octal number and I will convert it for you: 0
0
Sample Program Run (user input is underlined)
Enter up to an 8-digit octal number and I will convert it for you: 55
45
Sample Program Run (user input is underlined)
Enter up to an 8-digit octal number and I will convert it for you: 777777777
Enter up to an 8-digit octal number and I will convert it for you: 777777777
Enter up to an 8-digit octal number and I will convert it for you: 700000000
Enter up to an 8-digit octal number and I will convert it for you: 77
63
Assignment 4:
// Main.java : Java program to convert decimal number to octal
import java.util.Scanner;
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 " + inputNum + " is %07d in octal",convertedNum);
}
scan.close();
}
}
import java.util.Scanner;
public class OctalToDecimal {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter up to an
8-digit octal number and I will convert it for you: ");
int num = sc.nextInt();
conversion(num);
}
private static void conversion(int n) {
int num = n;
int dec_value = 0;
// starting from 1 as 8^0 is
1
int base = 1;
int temp = num;
while (temp > 0) {
// fetching last
digit using %
int last_digit =
temp % 10;
// removing last
digit
temp = temp /
10;
dec_value +=
last_digit * base;
// increseing
base to next like 8^1,8^2..
base = base *
8;
}
System.out.println(dec_value);
}
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
<terminated> Octal ToDecimal [Java Application] C:\Soft\PegaEclipse-win64-4.5.2.2\PegaEclipse-win64-4.5 Enter up to an 8-digit octal number and I will convert it for you: 775002 260610