-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise.java
More file actions
25 lines (20 loc) · 870 Bytes
/
Copy pathExercise.java
File metadata and controls
25 lines (20 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner in = new Scanner(System.in);
// Declare variables to store octal and decimal numbers, and an index counter
long octal_num, decimal_num = 0;
int i = 0;
// Prompt the user to input an octal number
System.out.print("Input any octal number: ");
octal_num = in.nextLong();
// Convert the octal number to decimal
while (octal_num != 0) {
decimal_num = (long)(decimal_num + (octal_num % 10) * Math.pow(8, i++));
octal_num = octal_num / 10;
}
// Display the equivalent decimal number
System.out.print("Equivalent decimal number: " + decimal_num + "\n");
}
}