Decimal to octal conversion using Java

Decimal to octal conversion using java

In this article we will discuss decimal to octal conversion using java. For this purpose we need to ask a decimal number from user and convert that decimal number to its octal equivalent form and then print the converted number on to the screen.
decimal to octal conversion

Methods Discussed:-

  • Using an array to store the comparable octal value
  • Using int variable to store comparable octal value

Algorithm  ( Method 1 )

  • Take the decimal number from the user and store it in variable say decimal.
  • Create an array say octal to store the octal representation of the given decimal number.
  • Run a loop until decimal is not equal to 0 i.e., (decimal !=0)
  • Inside the loop insert the remainder that achieved by dividing decimal from 8 i.e., (decimal%8)
  • Coming out from the loop print the octal array in reverse order.
Decimal to octal conversion using Java

Java Code

Run
//Java program to convert decimal number to octal number
import java.util.Scanner;
public class Main
{
	public static void main(String args[])
	{   
		//scanner class object creation
		Scanner sc = new Scanner(System.in); 
		//Number
		int decimal = 148;
		//integer array for storing octal digits
		int octal[] = new int[20];
		int i = 0; 
		//writing logic for the conversion 
		while(decimal > 0)
		{       
			int r = decimal % 8;
			octal[i++] = r;
			decimal = decimal/8;
		}
		//printing result
		System.out.print("Octal number : ");
		for(int j = i-1 ; j >= 0 ; j--)
		System.out.print(octal[j]);
		//closing scanner class(not compulsory, but good practice)
		sc.close();
	}
}

Output

224

Algorithm  ( Method 2 )

  • Initialize ocatalNum to 0 and countVal to 1 and the decimal number as n
  • Find the remainder when decimal number divided by 8
  • Update octal number by octalNum + (remainder * countval)
  • Increase countval by countval*10
  • Divide decimal number by 8
  • Repeat from the second step until the decimal number is zero

Java Code

Run
// JAVA program to convert decimal
import java.io.*;
public class Main {

    // function to calculate the octal value of the given
    // decimal number
    static void octaltodecimal(int deciNum)
    {

        int octalNum = 0, countval = 1;
        int dNo = deciNum;

        while (deciNum != 0) {

            // decimals remainder is calculated
            int remainder = deciNum % 8;

            // storing the octalvalue
            octalNum += remainder * countval;

            // storing exponential value
            countval = countval * 10;
            deciNum /= 8;
        }
        System.out.println(octalNum);
    }

    // Driver Code
    public static void main(String[] args)
    {

        int n = 33;

        // Function Call
        octaltodecimal(n);
    }
}

Output

41

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “Decimal to octal conversion using Java”


  • Mou

    import java.util.*;
    public class DecimalToOctal {
    public static int toOctal(int n)
    {
    String s = “”;
    while(n != 0)
    {

    int rem = n % 8;
    s = rem + s;
    n = n / 8;
    //System.out.println(s);

    }
    System.out.println(s);
    int m = Integer.parseInt(s);

    return m;
    }
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    toOctal(n);
    sc.close();

    }

    this is easy approach . try this !


  • Mayank Tambe

    import java.util.*;

    public class Main
    {
    public static void main(String[] args) {
    conversion obj=new conversion();
    obj.convert(214);
    }
    }
    class conversion{
    public void convert(int num){
    LinkedList ll = new LinkedList();
    while(num>0){
    int rem=num%8;
    ll.push(rem);
    num=num/8;
    }
    for (int i = 0; i < ll.size(); i++) {

    System.out.print(ll.get(i) + " ");
    }
    }
    }


  • swatikadas6

    import java.util.Scanner;

    class Beginner {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print(“Enter your number:”);
    int decimal = sc.nextInt();
    sc.nextLine();
    int count=0;
    int octal =0;
    int rem;
    int temp;
    double res;
    temp=decimal;
    while(temp!=0)
    {
    rem = temp%8;
    res = Math.pow(10,count);
    octal = (int) (octal+rem*res);
    temp/=8;
    count++;
    }
    System.out.println(“Octal Number “+octal);
    sc.close();
    }
    }