Skip to content

Commit ee0a282

Browse files
authored
Merge pull request ashutosh97#118 from martinezhenry/master
Added Octal Decimal Calculator
2 parents 089abdd + ea27233 commit ee0a282

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Given an octal number it is required to convert to decimal and print it on the console
2+
3+
Example:
4+
5+
The octal number 567 is entered.
6+
7+
The system calculates the equivalent number in decimal that is 375 and prints it on the console
8+
9+
The decimal number for octal 567 is: 357
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
public class OctalDecimalCalculator {
6+
7+
private static int BASE_8 = 8;
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final String RESULT_TEMPLATE = "The decimal number for octal %s is: %s";
10+
11+
public static void main(String[] args){
12+
13+
System.out.println("Please input octal number to convert");
14+
try {
15+
String octal = br.readLine();
16+
String decimal = convertOctalToDecimal(Integer.parseInt(octal));
17+
18+
System.out.println(String.format(RESULT_TEMPLATE, octal, decimal));
19+
} catch (IOException e) {
20+
e.printStackTrace();
21+
}
22+
23+
}
24+
25+
public static String convertOctalToDecimal(int octal){
26+
int result = 0;
27+
String tmp = String.valueOf(octal);
28+
int count = 0;
29+
int multiple;
30+
String caracter;
31+
for (int i = tmp.length(); i > 0; i--) {
32+
caracter = tmp.substring(i-1, i);
33+
multiple = (int) Math.pow(BASE_8, count);
34+
int value = (Integer.parseInt(caracter) * multiple);
35+
count = count + 1;
36+
result += value;
37+
38+
}
39+
40+
return String.valueOf(result);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)