w3resource

Java: Convert a decimal number to octal number

Java Basic: Exercise-21 with Solution

Decimal to Octal Converter

Write a Java program to convert a decimal number to an octal number.

Decimal number: The decimal numeral system is the standard system for denoting integer and non-integer numbers. It is also called base-ten positional numeral system.

Octal number: The octal numeral system is the base-8 number system, and uses the digits 0 to 7.

Pictorial Presentation: Decimal to Octal number

Java: Convert a decimal number to octal number

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise21 {
    public static void main(String args[]) {
        // Declare variables to store decimal number, remainder, quotient, and an array for octal digits
        int dec_num, rem, quot, i = 1, j;
        int oct_num[] = new int[100];
        
        // Create a Scanner object to read input from the user
        Scanner scan = new Scanner(System.in);

        // Prompt the user to input a decimal number
        System.out.print("Input a Decimal Number: ");
        dec_num = scan.nextInt();

        // Initialize the quotient with the decimal number
        quot = dec_num;

        // Convert the decimal number to octal and store octal digits
        while (quot != 0) {
            oct_num[i++] = quot % 8;
            quot = quot / 8;
        }

        // Display the octal representation of the decimal number
        System.out.print("Octal number is: ");
        for (j = i - 1; j > 0; j--) {
            System.out.print(oct_num[j]);
        }
        
        System.out.print("\n");
    }
}

Explanation:

In the exercise above -

  • It takes a decimal number ('dec_num') as input from the user using the "Scanner" class.
  • It initializes an array 'oct_num' to store the octal digits of the converted number and other necessary variables.
  • It enters a loop to perform the decimal-to-octal conversion:
    • In each iteration, it calculates the remainder of 'dec_num' when divided by 8 and stores it in the 'oct_num' array.
    • It then updates 'dec_num' by dividing it by 8 (which effectively shifts it to the right).
    • The loop continues until 'dec_num' becomes zero, effectively converting the entire decimal number to octal.
  • After the loop, it prints the octal representation of the decimal number by iterating through the 'oct_num' array in reverse order.

Sample Output:

Input a Decimal Number: 15                                                                                    
Octal number is: 17

Flowchart:

Flowchart: Java exercises: Convert a decimal number to octal number

Java Code Editor:

Previous: Write a Java program to convert a decimal number to hexadecimal number.
Next: Write a Java program to convert a binary number to decimal number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-21.php