Java Program to Convert Decimal To Octal

Write a Java program to convert decimal to octal. In this programming language, we can use the toOctalString method to convert decimal numbers to an octal string.

package Remaining;

public class DecimalToOctal1 {
	
	public static void main(String[] args) {
		
		int a = 81;
		int b = 27;
		int c = 125;
		int d = 175;
		
		System.out.println(Integer.toOctalString(a));
		System.out.println(Integer.toOctalString(b));
		System.out.println(Integer.toOctalString(c));
		System.out.println(Integer.toOctalString(d));
	}
}
121
33
175
257

This example accepts a decimal number and converts it to an octal string using the toOctalString.

package Remaining;

import java.util.Scanner;

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

		System.out.print("Please Enter Decimal Number =  ");
		int decimal = sc.nextInt();
	
		String OctalVal = Integer.toOctalString(decimal);
		System.out.println("Decimal To Octal Result   = " + OctalVal);
	}
}
Java Program to Convert Decimal To Octal 2

This program helps o convert decimal to octal using a while loop. 

package Remaining;

import java.util.Scanner;

public class DecimalToOctal3 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int temp, remainder;
		String octalVal = "";
		
		sc= new Scanner(System.in);

		System.out.print("Please Enter Number =  ");
		int decimal = sc.nextInt();
		
		char octalChars[] = {'0', '1', '2', '3', '4', '5', '6', '7'};
		
		temp = decimal;
		
		while(temp > 0)
		{
			remainder = temp % 8;
			octalVal = octalChars[remainder] + octalVal;
			temp = temp / 8;
		}
	
		System.out.println("Result   = " + octalVal);
	}
}
Please Enter Number =  125
Result   = 175

Another way of writing to convert decimal to octal.

package Remaining;

import java.util.Scanner;

public class DecimalToOctal4 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int temp, i = 1;
		int[] octalVal = new int[10];
		
		sc= new Scanner(System.in);

		System.out.print("Please Enter Number =  ");
		int decimal = sc.nextInt();
				
		temp = decimal;
		
		while(temp != 0)
		{
			octalVal[i++] = temp % 8;
			temp = temp / 8;
		}
	
		System.out.print(decimal + " Result   = ");
		for(int j = i - 1; j > 0; j--)
		{
			System.out.print(octalVal[j]);
		}
	}
}
Please Enter Number =  214
214 Result   = 326

This program converts decimal to octal using functions and the for loop.

package Remaining;

import java.util.Scanner;

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

		System.out.print("Please Enter Number =  ");
		int decimal = sc.nextInt();
		
		String octalVal = DecimalToOctal(decimal);
		System.out.println("Result   = " + octalVal);
	}
	
	public static String DecimalToOctal(int decimal)
	{
		int remainder;
		String octalVal = "";
		char octalChars[] = {'0', '1', '2', '3', '4', '5', '6', '7'};
		
		for(;decimal > 0; decimal = decimal / 8)
		{
			remainder = decimal % 8;
			octalVal = octalChars[remainder] + octalVal;
		}
		return octalVal;
	}
}
Please Enter Number =  95
Result   = 137