Java program to convert a decimal value to octal:
In this post, we will write one Java program to convert a decimal value to octal. The program will take the decimal value as input and print the converted octal value. Decimal is a base 10 number system. We use 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 to represent a number in Decimal. Octal is base 8 number system. To represent a number in octal, it uses 0, 1, 2, 3, 4, 5, 6 and 7.
Before we start writing down the program, let’s understand the algorithm to convert a decimal value to octal.
Algorithm to convert a decimal value to octal:
To convert a decimal number to octal, we will have to keep dividing the number by 8 and combine all the remainder in reverse to get the converted octal value.
The program will use the following algorithm:
- Take the decimal value as input from the user.
- If the value is less than or equal to 7, the octal representation is the same.
- Else, divide the number by 8. Note the remainder. Repeat the division with the quotient till it is zero.
- Now, combine the remainder in reverse to get the required octal value.
Example 1: How to use while loops to convert a decimal number to octal:
Let’s take one example to convert a decimal number to octal. Suppose, we want to convert 1254 to octal, the following table shows how it is done:
Decimal | Quotient of Decimal/8 | Remainder | Octal |
---|---|---|---|
1254 | 156 | 6 | 6 |
156 | 19 | 4 | 46 |
19 | 2 | 3 | 346 |
2 | 0 | 2 | 2346 |
So, 2346 is the octal conversion of 1254.
Java program to convert a decimal number to octal:
The below Java program converts a decimal value to octal:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int decimal, octal = 0, remainder;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the decimal number: ");
decimal = scanner.nextInt();
int multiplier = 1;
while (decimal != 0) {
remainder = decimal % 8;
octal += multiplier * remainder;
decimal /= 8;
multiplier *= 10;
}
System.out.println("Octal: " + octal);
}
}
Here,
- The integer variables decimal, octal and remainder are used to hold the decimal value, converted octal value and the remainder found while dividing the number by 8.
- The Scanner variable is used to read the user input decimal number.
- The integer variable multiplier is used to multiply with the remainder before it is added to the octal variable. It will be a multiple of 10 and it appends the remainder to the end of the octal variable.
- The while loop runs until the decimal value becomes zero. Inside the loop, it finds the remainder and adds it to the end of octal. The quotient of the division operation is assigned to the decimal variable on each iteration.
- At the end of the program, it prints the calculated octal variable.
Output of this program will be as below:
Enter the decimal number:
1254
Octal: 2346
Example 2: How to use a separate function to convert decimal to octal:
We can move the code snippet that converts the decimal to octal to a different function. The main function will call this function to get the converted value.
import java.util.Scanner;
class Main {
private static int decimalToOctal(int decimal) {
int octal = 0, remainder;
int multiplier = 1;
while (decimal != 0) {
remainder = decimal % 8;
octal += multiplier * remainder;
decimal /= 8;
multiplier *= 10;
}
return octal;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the decimal number: ");
int decimal = scanner.nextInt();
int octal = decimalToOctal(decimal);
System.out.println("Octal: " + octal);
}
}
The function decimalToOctal is a private static function that takes the decimal value as its parameter. It converts it to octal and returns it. This main function is calling this new function. You will get a similar result.
Example 3: How to use for loops to convert a decimal value to octal:
For a for loop, we have to give three conditions:
- Initialization
- Condition that will be checked on each iteration
- Code to execute at the end of each iteration.
We can,
- Initialize the multiplier variable as the initialization.
- Check for the current decimal value as the second condition of the loop.
- Update the multiplier and decimal values at the end of each iteration.
import java.util.Scanner;
class Main {
private static int decimalToOctal(int decimal) {
int octal = 0;
for (int multiplier = 1; decimal != 0; decimal /= 8, multiplier *= 10) {
octal += (decimal % 8) * multiplier;
}
return octal;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the decimal number: ");
int decimal = scanner.nextInt();
int octal = decimalToOctal(decimal);
System.out.println("Octal: " + octal);
}
}
- The multiplier is initialized as 1,
int multiplier = 1
- It will run until the decimal value is not equal to zero,
decimal != 0
- At the end of each iteration, the decimal and multiplier values are updated.
decimal /= 8, multiplier *= 10
It will give similar results.
Enter the decimal number:
1564
Octal: 3034
Example 4: Decimal to octal string with Integer.toOctalString function:
The Integer class provides a function called toOctalString to convert a decimal value to octal string. This is defined as below:
public static String toOctalString(int i)
It is a static method. It takes an integer as its parameter and returns the converted octal value as string.
Let’s try this function:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the decimal number: ");
int decimal = scanner.nextInt();
String octal = Integer.toOctalString(decimal);
System.out.println("Octal: " + octal);
}
}
It will print output as below:
Enter the decimal number:
1342
Octal: 2476
Enter the decimal number:
3342
Octal: 6416
You can use any of the methods we discussed above but using this inbuilt function is easier than writing your own.
You might also like:
- Java program to find the power of a number using recursion
- Java program to calculate compound interest
- 3 ways in Java to print Hello World without semicolon
- 3 ways in Java to print a reverse half pyramid pattern
- Java program to find the largest and smallest of three numbers
- Java vector addAll method explanation with example
- Java program to find the largest and smallest of n user input numbers
- Java program to print left-aligned and right-aligned staircase patterns