BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program for Decimal to Octal Conversion

Last Updated: November 2, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this guide, we will discuss the following two ways to convert decimal to octal in Java.

  • Using predefined method toOctalString(int num) of Integer class
  • Writing a custom logic for decimal to octal conversion

Example 1: Decimal to Octal using toOctalString() method

The Integer.toOctalString(int i) method accepts integer number as argument and returns the equivalent octal number as string after conversion. The syntax of toOctalString() is as follows:

public static String toOctalString(int i)

Let’s write a java program for conversion:

public class JavaExample{
  public static void main(String args[]){
    int decimalNumber = 80;
    String octalNumber = Integer.toOctalString(decimalNumber);
    System.out.println("Decimal to Octal: "+octalNumber);
  }
}

Output:

Decimal to Octal: 120

Example 2: Conversion without using toOctalString()

Here, we are not using the predefined method for conversion. We have used a logic to convert the given decimal number to octal.

class DecimalToOctalExample
{
  public static void main(String args[])
  {
    int num = 101;

    // For storing remainder
    int rem;

    // For storing result
    String str="";

    // Digits in Octal number system
    char dig[]={'0','1','2','3','4','5','6','7'};

    while(num>0)
    {
      rem=num%8;
      str=dig[rem]+str;
      num=num/8;
    }
    System.out.println("Decimal to Octal: "+str);
  }
}

Output:

Decimal to Octal: 145

Recommended Posts

  • Java Octal to Decimal Conversion
  • Java Decimal to Hexadecimal Conversion
  • Java Decimal to Octal Conversion
  • Java Decimal to Binary Conversion
❮ Java Programs

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program for Decimal to Hexadecimal Conversion
  3. Fascinating Number Program in Java
  4. Java Program to Display Fibonacci Series using loops
  5. Java Program to print Pascal Triangle

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap