forked from raman1200/community_issues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Octal to Decimal Conversion Program
45 lines (36 loc) · 1.47 KB
/
Octal to Decimal Conversion Program
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
public class OctalToDecimalConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter an octal number
System.out.print("Enter an octal number: ");
String octalNumber = scanner.nextLine();
// Validate if the input is a valid octal number
if (!isValidOctal(octalNumber)) {
System.out.println("Invalid octal number.");
} else {
// Convert octal to decimal
int decimalNumber = octalToDecimal(octalNumber);
System.out.println("Decimal equivalent: " + decimalNumber);
}
scanner.close();
}
// Function to check if a string is a valid octal number
public static boolean isValidOctal(String octal) {
// A valid octal number consists of digits 0-7
return octal.matches("^[0-7]+$");
}
// Function to convert an octal number to decimal
public static int octalToDecimal(String octal) {
int decimalNumber = 0;
int length = octal.length();
// Convert each digit from right to left
for (int i = 0; i < length; i++) {
char digit = octal.charAt(i);
int digitValue = digit - '0'; // Convert char to integer
// Multiply the current decimal number by 8 and add the current digit's value
decimalNumber = decimalNumber * 8 + digitValue;
}
return decimalNumber;
}
}