Finally, let's modify the program to allow user input:
import java.util.Scanner;
public class LongToOctal {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a long integer: ");
long input = sc.nextLong();
String octal = Long.toOctalString(input);
System.out.println("Octal representation of " + input + ": " + octal);
}
}
Here, we first import the Scanner
class to read user input from the console. The program prompts the user to enter a long integer, reads the input using the Scanner
class, and then uses the toOctalString()
method to convert it to its equivalent octal representation. The resulting octal representation is then printed to the console.