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

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

Leave a Reply Cancel reply

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

Programs

  • C Programs
  • Java Programs
  • C++ Programs

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 – 2022 BeginnersBook . Privacy Policy . Sitemap