×

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal:
  • Using toOcatlString() method
  • Using user-defined logic
Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer wrapper class that converts decimal to an octal string. The signature of the method is given below: public static String toOctalString(int decimal) Example In the following example, we have invoked the toOctalString() method of Integer wrapper class. The method parses an integer argument which you want to convert and returns the octal representation of the decimal number.
public class DecimalToOctalExample
{
public static void main(String args[])
{
System.out.println(Integer.toOctalString(21));
System.out.println(Integer.toOctalString(67));
}
}
Output
25
103
Using user-defined logic You can also convert decimal to octal in using user-defined logic. Example In the following example, we have defined a method of name conversion. Inside the method body, we have taken two variables rem (represent remainder) of type integer, and octal (represent octal number) of type String. We have also taken an array octalchar[]of characters, and assign values from '0'to '7' into it. The method has a while loop that checks the condition and performs the iterations. Let’s see how the while loop will be executed. Remember: The second statement of the while loop performs concatenation instead of addition.If you change the sequence of the statement octal=octal+octalchars[rem]then the output will be 41 which is the wrong output. So remember the sequence while you write this statement. For first iteration decimal=12                                                                                         //passed parameter 12>0                                                                                                        //condition true rem=12%8 = 4                                                                                   //finds remainder octal=octalchars[4]+octal = 4                                      //updated value of octal is 4 decimal=12/8 = 1                                                              //updated value of decimal is 1 For second iteration decimal=1 1>0                                                                                                           //conditiontrue rem=1%8 = 1                                                                                      //finds remainder octal=octalchars[1]+octal = 14                                    //updated value of octal is 4 decimal=1/8 = 0                                                                                //updated value of decimal is 0 For third iteration decimal=0 0>0                                                                                                         //condition false The while loop terminates the execution, and returns the converted octal value. Similarly you can also perform iteration for the decimal value 75. Hence the third iteration will not be executed. The while loop terminates the execution. The method conversion returns the octalvalue.
public class DecimalToOctalExample1
{
public static String conversion(int decimal)                           //called method
{
int rem;
String octal="";
char octalchars[]={'0','1','2','3','4','5','6','7'};
while(decimal>0)
{
rem=decimal%8;
octal=octalchars[rem]+octal;
decimal=decimal/8;
}
return octal;
}
public static void main(String args[])
{
System.out.println("Decimal to octal of 12 is: "+conversion(12));                                               //methodcalling
System.out.println("Decimal to octal of 75 is: "+conversion (75));
}
}
Output
Decimal to Octal of 12 is: 14
Decimal to octal of 75 is: 113

Related Topics

Java Sort String

In this article, you will be acknowledged about how to sort a string in Java. Introduction Firstly, let us revise what is string Strings are collections of characters that are frequently used in...

4 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.