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

Java Program to Convert Octal to Decimal 2022 (updated)

0
86

Java Program to Convert Octal to Decimal 2022 ( updated ) – Generally Octal number system used to store big data values. In the previous post, we had developed a Java program to convert decimal to octal value. Now in this post, we will develop a Java program to convert the octal value to decimal value.

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


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

<img decoding=

Procedure to develop a method to convert octal to decimal,

  • Take an octal number
  • Declare variables decimal, remainder, multiplier, and iteration variable
  • Calculate the remainder after dividing by base of decimal that is 10
  • Calculate the multiplier value. In each iteration multiplier = 8^i
  • Increase decimal value as, decimal = decimal + (remainder * multiplier)
  • Update octal number through dividing it by 10
  • Increase the iteration variable by 1
  • Repeat 3 to 7 steps until the octal number becomes 0

Java Program to Convert Octal to Decimal

 

import java.util.Scanner;

public class NumberConversion {
  private static int toDecimal(int octal) {

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

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

        // calculate multiplier value
        multiplier = (int) Math.pow(8, i);
        // increase decimal value
        decimal += (remainder * multiplier);

        // divide decimal value by 8
        octal /= 10;
        // increase i value by 1
        i++;
     }

     return decimal;
  }

  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 input
     System.out.print("Enter octal number::");
     octal = scan.nextInt();

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

     // display result
     System.out.println("Decimal value = " 
                              + decimal);

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

OUTPUT


Enter octal number:: 12
Decimal 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 !

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here