Here we are going to write a program on conversion of Decimal To Octal Numbers in Java.
Decimal To Octal Conversion :
ConvertDecimalToOctal.java
import java.util.Scanner;
public class ConvertDecimalToOctal {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter any Decimal Number : ");
int input = sc.nextInt();
System.out.println("Oct is : "+convertDecimalToOctal(input));
}
public static String convertDecimalToOctal(int decimal) {
String octal = "";
try {
if (decimal != 0) {
while (decimal > 0) {
octal = decimal % 8 + octal;
decimal /= 8;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return octal;
}
}
Output :
Enter any Decimal Number : 156
Oct is : 234
Happy Learning 🙂