-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbnary_to_deci.java
52 lines (49 loc) · 1.04 KB
/
bnary_to_deci.java
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
49
50
51
52
/**
* Write a description of class bnary_to_deci here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class bnary_to_deci
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter?");
String str=sc.nextLine();
double sum=0;
for(int i=0;i<str.length();i++)
{
int t = (str.length()-1);
t-=i;
if(str.charAt(i)=='1')
{
sum=sum+Math.pow(2,t);
}
}//cnversion to binary
int s=(int)(sum);
int n=s;
int h=0;
int c=0;
while(n>0)
{
int a=n%8;
h=h*10+a;
n=n/8;
c++;
}
int j=h;
int b=0;
for(int i=0;i<=c;i++)
{
while(j>0)
{
int a=j%10;
b=b*10+a;
j=j/10;
}
}
System.out.println(b);
}
}