Java Code Example: convert decimal to octal number

To convert a number from the decimal system to the octal system, the toOctalString() method is used in Java. The Integer class provides several methods, such as the conversion methods.

import java.util.Scanner;

public class ConvertToOctal {
  public static void main(String[] args) {
    int num;

    Scanner s = new Scanner(System.in);
    System.out.print("Please enter a number: ");

    num = s.nextInt();

    s.close();

    String oct = Integer.toOctalString(num);
    System.out.println("Octal value of number " + num + " is: " + oct);
  }
}
Output
Please enter a number: 15
Octal value of number 15 is: 17