Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Statement Prompt the user for an integer value in decimal and convert th

ID: 3802344 • Letter: P

Question

Problem Statement
Prompt the user for an integer value in decimal and convert the value to it's octal (base 8) equivalent value. Display the result. The program should prompt the user for another value to convert and terminate if the user enters 0 (zero). A sample session might look like this:

Enter an integer or 0 to quit: 29
The value in octal is 35
Enter an integer or 0 to quit: 105
The value in octal is 151
Enter an integer or 0 to quit: 0
Thank you!

Notes:

Implement using a call to a method to perform the conversion

Use Java or a language of your choice.

You may implement using either a loop or recursion (extra credit if you use recursion)

Explanation / Answer

Here is the code to convert decimal number to octal via using recursion

import java.util.*;
import java.lang.*;
import java.io.*;

class DecimalToOctal
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no:");
int n = sc.nextInt();
if(n!=0){
System.out.println(" Decimal to Octal:");
convertDecimalToOctal(n);
}
else{
System.out.println(" Cannot convert the entered decimal number to Octal:");
System.exit(0);
}
}
public static void convertDecimalToOctal(int n)
{
if (n > 0)
{
convertDecimalToOctal(n / 8);
System.out.printf("%d", n % 8);
}
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote