Java Program to Convert Octal To Decimal

Write a Java program to convert octal to decimal. In this Java octal to decimal conversion example, we use the integer parseInt method with octal string as the first and eight as the second arguments.

package Remaining;

public class OctalToDecimal1 {
	
	public static void main(String[] args) {
		
		String s1 = "10";
		String s2 = "67";
		String s3 = "125";
		String s4 = "142";
		
		System.out.println(Integer.parseInt(s1, 8));
		System.out.println(Integer.parseInt(s2, 8));
		System.out.println(Integer.parseInt(s3, 8));
		System.out.println(Integer.parseInt(s4, 8));
	}
}
8
55
85
98

This Java example accepts the octal string and converts it to a decimal number using the parseInt function.

package Remaining;

import java.util.Scanner;

public class OctalToDecimal2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc= new Scanner(System.in);

		System.out.print("Please Enter Octal Number =  ");
		String octalString = sc.nextLine();
	
		int decimalVal = Integer.parseInt(octalString, 8);
		System.out.println("Octal To Decimal Result = " + decimalVal);
	}
}
Java Program to Convert Octal To Decimal

Java program to convert octal to decimal using a while loop. 

package Remaining;

import java.util.Scanner;

public class OctalToDecimal3 {
	private static Scanner sc;
	
	public static void main(String[] args) {

		int  octalVal, temp, remainder, decimal = 0, n = 0;
		sc= new Scanner(System.in);

		System.out.print("Please Enter Number =  ");
		octalVal = sc.nextInt();
		
		temp = octalVal;
		while(temp > 0)
		{
			remainder = temp % 10;
			decimal = (int) (decimal + remainder * Math.pow(8, n));
			temp = temp / 10;
			n++;
		}

		System.out.println(octalVal + " Result = " + decimal);
	}
}
Please Enter Number =  128
128 Result = 88

Java program to convert octal to decimal using functions.

package Remaining;

import java.util.Scanner;

public class OctalToDecimal4 {
	private static Scanner sc;
	
	public static void main(String[] args) {

		sc= new Scanner(System.in);

		System.out.print("Please Enter Octal Number =  ");
		int octalVal = sc.nextInt();
		
		int decimal = octalToDecimal(octalVal);

		System.out.println("Decimal Result = " + decimal);
	}
	
	public static int octalToDecimal(int octalVal)
	{
		int decimal = 0, i = 0;
		
		while(octalVal != 0)
		{
			decimal = (int) (decimal + (octalVal % 10) * Math.pow(8,  i++));
			octalVal = octalVal / 10;
		}
		return decimal;
	}
}
Please Enter Octal Number =  1129
Decimal Result = 601