Skip to content

Commit cc85e6e

Browse files
authored
Added binary to decimal program
1 parent b9a3f49 commit cc85e6e

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

binarytodecimal.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#https://www.facebook.com/100028679802914/posts/449717249327598/
3+
#subcribed by Nikita Desale
4+
class GFG {
5+
static int binaryToDecimal(int n)
6+
{
7+
int num = n;
8+
int dec_value = 0;
9+
10+
// Initializing base
11+
// value to 1, i.e 2^0
12+
int base = 1;
13+
14+
int temp = num;
15+
while (temp > 0) {
16+
int last_digit = temp % 10;
17+
temp = temp / 10;
18+
19+
dec_value += last_digit * base;
20+
21+
base = base * 2;
22+
}
23+
24+
return dec_value;
25+
}
26+
27+
// Driver Code
28+
public static void main(String[] args)
29+
{
30+
int num = 10101001;
31+
System.out.println(binaryToDecimal(num));
32+
}
33+
}

0 commit comments

Comments
 (0)