Java Program to Converter a Decimal to Octal
You are Here:
Converter a Number from Decimal to Octal
In the following example, we will convert a Decimal Number (100) to Octal Number (144).
Tips: It is recommended to use our online Decimal to Octal calculator for better understanding.
Example
Java Compiler
import java.lang.Math;
public class myClass
{
public static void main(String[] args)
{
int num = 23456;
int[] arr = new int[50];
int[] answer = new int[50];
int value = 1;
int i, j = 0;
int count = -1;
System.out.print("Octal number of 23456 (decimal) is ");
while(num > value)
{
count++;
value = (int) Math.pow(8, count);
arr[count] = value;
}
count = count - 1;
for(i=count; i>=0; i--)
{
answer[j] = num / arr[i];
j++;
num = num % arr[i];
}
for(i=0; i<=count; i++)
System.out.print(answer[i]);
}
}
Output
Octal number of 23456 (decimal) is 55640
Converter any Given Decimal Number to Octal Number
In the following example, we will convert any given decimal number to an octal number.
Example
Java Compiler
import java.util.Scanner;
import java.lang.Math;
public class myClass
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int num, copyNum;
int[] arr = new int[50];
int[] answer = new int[50];
int value = 1;
int i, j = 0;
int count = -1;
System.out.print("Enter a Decimal number: ");
num = reader.nextInt();
copyNum = num;
while(copyNum > value)
{
count++;
value = (int) Math.pow(8, count);
arr[count] = value;
}
count = count - 1;
for(i=count; i>=0; i--)
{
answer[j] = copyNum / arr[i];
j++;
copyNum = copyNum % arr[i];
}
System.out.format("Octal number of %d (decimal) is ", num);
for(i=0; i<=count; i++)
System.out.print(answer[i]);
}
}
Output
Enter a Decimal number: 10
Octal number of 10 (decimal) is 12
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.
We are working to cover every Single Concept in Java.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.