Skip to content

Commit 13ded5c

Browse files
authored
binary to decimal
1 parent fb425ac commit 13ded5c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

binary_to_decimal.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// convert binary to decimal
2+
3+
#include <stdio.h>
4+
#include <math.h>
5+
6+
// function prototype
7+
int convert(long long);
8+
9+
int main() {
10+
long long n;
11+
printf("Enter a binary number: ");
12+
scanf("%lld", &n);
13+
printf("%lld in binary = %d in decimal", n, convert(n));
14+
return 0;
15+
}
16+
17+
// function definition
18+
int convert(long long n) {
19+
int dec = 0, i = 0, rem;
20+
21+
while (n!=0) {
22+
rem = n % 10;
23+
n /= 10;
24+
dec += rem * pow(2, i);
25+
++i;
26+
}
27+
28+
return dec;
29+
}

0 commit comments

Comments
 (0)