-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.java
More file actions
48 lines (45 loc) · 1.18 KB
/
Binary.java
File metadata and controls
48 lines (45 loc) · 1.18 KB
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
46
47
48
/** Binary to Decimal Number converter using recursion
* Binary equivalent of 69 is 1000101
*/
import java.util.*;
public class Binary
{
long bin,dec;
void read()
{
Scanner as=new Scanner(System.in);
System.out.print("Enter the Binary Number:");
bin=as.nextLong();
if(check(bin)==false)
{
System.out.println("The Binary number is invalid, pls try again.");
read();
}
}
boolean check(long binC)
{
int l=String.valueOf(binC).length();
for(int i=1;i<=l;i++,binC/=10)
if(binC%10!=0 && binC%10!=1)
return false;
return true;
}
long convert(long ka,int l)
{
if(ka>0)
dec=((ka%10)*(int)Math.pow(2,l))+convert(((long)ka/10),(l+1));
return dec;
}
void show()
{
System.out.println("\nThe Binary number is:"+bin);
System.out.println("The Equivalent decimal form is:"+convert(bin,0));
}
public static void main(String[] args)
{
Binary as1=new Binary();
System.out.println("\t-Binary to Decimal Converter-\n");
as1.read();
as1.show();
}
}