-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryAndDecimal.groovy
More file actions
42 lines (36 loc) · 995 Bytes
/
binaryAndDecimal.groovy
File metadata and controls
42 lines (36 loc) · 995 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
boolean b2d = false
boolean d2b = false
print "Would you like to convert a binary number to decimal?(b2d) or a decimal number to binary(d2b): "
String str = System.console().readLine()
while(b2d==false && d2b==false){
if(str == "b2d"){
b2d = true
} else if(str == "d2b"){
d2b = true
} else {
println "You have entered an invalid statement, Please try again"
str = null
str = System.console().readLine()
}
}
String binaryNum
if(b2d == true){
print "Please enter a binary number: "
binaryNum = System.console().readLine()
}
double binary2decimal(String binaryNumx){
double decimal
if(binaryNumx.charAt(0) == "1"){
decimal = decimal + 1
}
for(int a = 0; a < binaryNumx.length();a++){
if(binaryNumx.charAt(a) == "1"){
decimal = decimal + 1*2*a
println decimal
}
}
return decimal
}
double returnDecimal = binary2decimal(binaryNum)
//double returnBinary = decimal2binary(int
print returnDecimal