forked from zahinekbal/codeWith-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary2DecimalBasic.c
More file actions
47 lines (37 loc) · 745 Bytes
/
binary2DecimalBasic.c
File metadata and controls
47 lines (37 loc) · 745 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
43
44
45
46
47
/*
# Program for Binary To Decimal Conversion
Given a binary number as input, we need to write a program to convert the given binary number into an equivalent decimal number.
Examples :
Input : 111
Output : 7
Input : 1010
Output : 10
Input: 100001
Output: 33
*/
#include <stdio.h>
int bin2dec(int num)
{
int dec = 0, base = 1;
while (num != 0)
{
dec = dec + ((num % 10) * base);
base *= 2;
num /= 10;
}
return dec;
}
int main()
{
// for test case
int t;
scanf("%d", &t);
while (t--)
{
// main logic
int num;
scanf("%d", &num);
printf("%d\n", bin2dec(num));
}
return 0;
}