Convert decimal to octal in Java
In this post, you will learn how to convert decimal to octal using Java programming.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of Decimal Number System are easy-to-read, used by humans and easy to manipulate. But there are some disadvantages, like wastage of space and time.
The octal number system has a base of eight like 238, 1028, 418, etc. and uses the number from 0 to 7. The octal number system is widely used in computer application sectors and digital numbering systems. The main advantage of using Octal numbers is that it uses less digits than decimal and Hexadecimal number system. That's why it has fewer computations and less computational errors.
Java program to convert decimal to octal using Integer.toOctalString()
Java provides a built-in Integer.toOctalString() function to convert various values to a octal number. This function returns octal value.
// Java program to convert decimal to octal
public class DecimalToOctalExample1{
public static void main(String args[]){
// convert decimal value into octal
System.out.println("Octal value of 18 : "+Integer.toOctalString(18));
System.out.println("Octal value of 4 : "+Integer.toOctalString(4));
System.out.println("Octal value of 15 : "+Integer.toOctalString(15));
System.out.println("Octal value of 12 : "+Integer.toOctalString(12));
System.out.println("Octal value of 91 : "+Integer.toOctalString(91));
}
}
Output of the above code:
Octal value of 18 : 22
Octal value of 4 : 4
Octal value of 15 : 17
Octal value of 12 : 14
Octal value of 91 : 133
Java program to convert decimal to octal using while loop
Here, we have applied custom logic to convert decimal to octal in Java.
// Java program to convert decimal to octal
public class ConvertDecimalToOctal{
// creating function for conversion
public static String decimalToOctal(int decimal){
//declaring variable to store remainder
int rem;
//declaring variable to store octal
String octal="";
// declaring array of octal numbers
char octalchars[]={'0','1','2','3','4','5','6','7'};
// logic for decimal to octal conversion
while(decimal>0)
{
// Calculating remainder
rem=decimal%8;
// storing the octal value
octal=octalchars[rem]+octal;
decimal=decimal/8;
}
return octal;
}
public static void main(String args[]){
// Calling function to get the octal number of given decimal value
System.out.println("Decimal to octal of 8 is: "+decimalToOctal(8));
System.out.println("Decimal to octal of 19 is: "+decimalToOctal(19));
System.out.println("Decimal to octal of 81 is: "+decimalToOctal(81));
System.out.println("Decimal to octal of 23 is: "+decimalToOctal(23));
System.out.println("Decimal to octal of 91 is: "+decimalToOctal(91));
}
}
Output of the above code:
Decimal to octal of 8 is: 10
Decimal to octal of 19 is: 23
Decimal to octal of 81 is: 121
Decimal to octal of 23 is: 27
Decimal to octal of 91 is: 133
Related Articles
Capitalize first letter of each word JavaConvert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number