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

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

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022