Skip to content

Commit e8dcd1f

Browse files
authored
binary to decimal
I added a code of conversion of binary to decimal
1 parent 405d8bc commit e8dcd1f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Binary to Decimal

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <conio.h>
3+
void main()
4+
{
5+
// declaration of variables
6+
int num, binary_num, decimal_num = 0, base = 1, rem;
7+
printf (" Enter a binary number with the combination of 0s and 1s \n");
8+
scanf (" %d", &num); // accept the binary number (0s and 1s)
9+
10+
binary_num = num; // assign the binary number to the binary_num variable
11+
12+
13+
while ( num > 0)
14+
{
15+
rem = num % 10; /* divide the binary number by 10 and store the remainder in rem variable. */
16+
decimal_num = decimal_num + rem * base;
17+
num = num / 10; // divide the number with quotient
18+
base = base * 2;
19+
}
20+
21+
printf ( " The binary number is %d \t", binary_num); // print the binary number
22+
printf (" \n The decimal number is %d \t", decimal_num); // print the decimal
23+
    getch();  
24+
}

0 commit comments

Comments
 (0)