Java Program to Convert Octal Number to Decimal and vice-versa

In this Java tutorial, you’ll learn how to Convert Octal Number to Decimal and vice-versa using the Java programming language.   

How to Convert Octal Number to Decimal and vice-versa using JAVA? 

RUN CODE SNIPPET
public class Main 
{ 
    public static void main(String[] args)  
    { 
        int decimal = 78; 
        int octal = convertDecimalToOctal(decimal); 
        System.out.printf("%d in decimal = %d in octal", decimal, octal); 
    } 
    public static int convertDecimalToOctal(int decimal) 
    { 
        int octalNumber = 0, i = 1; 
        while (decimal != 0) 
        { 
            octalNumber += (decimal % 8) * i; 
            decimal /= 8; 
            i *= 10; 
        } 
        return octalNumber; 
    } 
}

OUTPUT 

78 in decimal = 116 in octal

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024