 # Java Program to convert decimal number to octal number

Here we are explaining how to write a java program to convert decimal numbers to octal numbers.

## What is the decimal number?

A decimal number is a base-ten number. A decimal number has mainly two parts. A fraction part and an integer part. The fraction part is separated by a dot.

Example:57.3

5

63.52

## How to implement the java program to convert decimal numbers to octal numbers in java?

First, we have to declare the class DecToOct.Declare the integer variable r and string variable oct to hold the octal number. Create an object of the scanner class as sc. Read the decimal number from the user into the variable dec. Keep the decimal number into another variable  dec1. Declare the character array OC[].By using `while loop` check dec>0,then calculate r=dec%8,oct=OC[r]+oct,dec=dec/8.Now the variable oct contains the octal number. Display oct by using `system.out.prin`tln in java.

## ALGORITHM

STEP 1: Declare the class DecToOct with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3:Declare the integer variable r and string variable oct.

STEP 4: Create an object of the scanner class as sc.

STEP 5: Read the decimal number from the user into the variable dec.

STEP 6: Assign dec1=dec.

STEP 7: Declare the character array OC[].

STEP 8: By using `while loop` checks if dec>0 if true then break else do step 9.

STEP 9: r=dec%8.

STEP 10: Calculate oct=OC[r]+oct,dec=dec/8.

STEP 11: Repeat step 8.

STEP 12: Display the octal equivalent of the decimal number as oct.

## Java Source Code

```                                          ```import java.util.Scanner;
public class DecToOct{
public static void main(String args[]){
int r;
String oct="";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the decimal number:");
int dec= sc.nextInt();

int dec1=dec;

char OC[]={'0','1','2','3','4','5','6','7'};

while(dec>0)
{
r=dec%8;
oct=OC[r]+oct;
dec=dec/8;
}

System.out.println("The Octal Equivalent of the decimal number "+dec1+" is " +oct);
}
}```
```

## OUTPUT

```Enter the decimal number:9
The Octal Equivalent of the decimal number 9 is 11.```