From 96a41156fbb749a891f123f4e85d49605fdba2b7 Mon Sep 17 00:00:00 2001 From: Shreya Mishra <91959996+Shreya-Lu@users.noreply.github.com> Date: Mon, 31 Oct 2022 23:37:02 +0530 Subject: [PATCH] Create Binary2Decimal.c Binary number to decimal number conversion using while loop --- Binary2Decimal.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Binary2Decimal.c diff --git a/Binary2Decimal.c b/Binary2Decimal.c new file mode 100644 index 0000000..91034e6 --- /dev/null +++ b/Binary2Decimal.c @@ -0,0 +1,24 @@ +#include +#include +void main() +{ + // declaration of variables + int num, binary_num, decimal_num = 0, base = 1, rem; + printf (" Enter a binary number with the combination of 0s and 1s \n"); + scanf (" %d", &num); // accept the binary number (0s and 1s) + + binary_num = num; // assign the binary number to the binary_num variable + + + while ( num > 0) + { + rem = num % 10; /* divide the binary number by 10 and store the remainder in rem variable. */ + decimal_num = decimal_num + rem * base; + num = num / 10; // divide the number with quotient + base = base * 2; + } + + printf ( " The binary number is %d \t", binary_num); // print the binary number + printf (" \n The decimal number is %d \t", decimal_num); // print the decimal + getch(); +}