This Java program converts a given number into its equivalent octal representation. Here’s how the code works:
Scanner
class, which is used to read input from the user.ConvertToOctal
is defined that contains a main
method.num
of type int
is declared to store the input number that the user will provide.Scanner
object s
is created to read input from the user. The program prompts the user to enter a number by using System.out.print
method.nextInt
method of the Scanner
class and stored in the variable num
.Scanner
object is closed using the close
method.toOctalString
method of the Integer
class, which takes the input number as an argument and returns the octal equivalent as a string.oct
.num
, the string ” is: ” with the value of oct
.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);
}
}
Please enter a number: 15
Octal value of number 15 is: 17