Home Lifestyle Java Program to Convert Decimal to Octal (updated)

Java Program to Convert Decimal to Octal (updated)

98
0

Java Program to Convert Decimal to Octal (updated) – In Computer Science, generally Octal number system used to store big data values. In this post, we will develop a Java program to convert the decimal value to an octal value.

  • Decimal Octal
  • 1 1
  • 2 2
  • 3 3
  • 4 4
  • 5 5
  • 6 6
  • 7 7
  • 8 10
  • 9 11
  • 10 12

<img decoding=

In Octal number system 8 and 9 are not available because their base is 8.

  • Procedure to develop a method to convert decimal to octal,
  • 1) Take a decimal number
  • 2) Declare octal, remainder, multiplier, and iteration variables

Java Program to Convert Decimal to Octal

import java.util.Scanner;

public class DecimalToOctal {
  private static int toOctal(int decimal) {

     // declare variables
     int octal = 0;
     int remainder = 0;
     int multiplier = 0;
     int i = 0;

     while (decimal!=0) {
        // find remainder after
        // dividing by 8
        remainder = decimal % 8;

        // calculate multiplier value
        multiplier = (int) Math.pow(10, i);

        // increase octal value
        octal += (remainder * multiplier);
        // divide decimal value by 8
        decimal /= 8;

        // increase i value by 1
        i++;
     }
     return octal;
  }

  public static void main(String[] args) {
     // declare variables
     int decimal = 0;
     int octal = 0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read inputs
     System.out.print("Enter decimal number:: ");
     decimal = scan.nextInt();

     // convert decimal to octal
     octal = toOctal(decimal);

     // display result
     System.out.println("Octal value = "
                            + octal);

     // close Scanner class object
     scan.close();
  }
}

OUTPUT


Enter decimal number:: 8
Octal value = 10

Types of Sorting Algorithms:

Additional Reading

READ MORE

If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.

Thank you 😊 Keep Learning !

Previous articleProgram to Convert Fahrenheit to Celsius in Java Language (updated)
Next articleJava Program to Convert Octal to Decimal 2022 (updated)

LEAVE A REPLY

Please enter your comment!
Please enter your name here