-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctalToDecimal.java
More file actions
43 lines (38 loc) · 816 Bytes
/
OctalToDecimal.java
File metadata and controls
43 lines (38 loc) · 816 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Scanner;
public class OctalToDecimal{
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter a Number");
int no=sc.nextInt();
int dec=OctalToDecimal(no);
if (no<0) {
System.out.println("Enter a number which is greater than or equal to 0");
}
else if(dec!=-1) {
System.out.println(dec+" is Decimal Number & "+no+" is Octal Number");
}
}
public static int OctalToDecimal(int no) {
int dec=0;
int i=0;
while(no>0) {
int last=no%10;
if (last>7) {
System.out.println("Wrong Input Please try again");
return -1;
}
no/=10;
dec=Power(i)*last+dec;
i++;
}
return dec;
}
public static int Power(int pow){
int power=1;
while(pow>0){
power*=8;
pow--;
}
return power;
}
}