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.out.print("Please enter a number: ");

    num = s.nextInt();


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