2017-07-13 07:59:31 +08:00
|
|
|
/**
|
2023-05-29 23:09:21 +08:00
|
|
|
* Modified 24/05/2023, Indrranil Pawar
|
|
|
|
*
|
|
|
|
* C program that converts a binary number to its decimal equivalent.
|
|
|
|
*/
|
2017-07-13 07:59:31 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-05-30 04:23:24 +08:00
|
|
|
int main()
|
|
|
|
{
|
2023-05-29 23:09:21 +08:00
|
|
|
int binary_number, decimal_number = 0, temp = 1;
|
2017-07-13 07:59:31 +08:00
|
|
|
|
2023-05-29 23:09:21 +08:00
|
|
|
// Input the binary number
|
|
|
|
printf("Enter any binary number: ");
|
|
|
|
scanf("%d", &binary_number);
|
|
|
|
|
|
|
|
// Convert binary to decimal
|
|
|
|
while (binary_number > 0)
|
2020-05-30 04:23:24 +08:00
|
|
|
{
|
2023-05-29 23:09:21 +08:00
|
|
|
// Extract the rightmost digit of the binary number
|
|
|
|
int digit = binary_number % 10;
|
|
|
|
|
|
|
|
// Multiply the rightmost digit with the corresponding power of 2 and add to the decimal number
|
|
|
|
decimal_number += digit * temp;
|
|
|
|
|
|
|
|
// Remove the rightmost digit from the binary number
|
|
|
|
binary_number /= 10;
|
|
|
|
|
|
|
|
// Increase the power of 2 for the next digit
|
|
|
|
temp *= 2;
|
2020-05-30 04:23:24 +08:00
|
|
|
}
|
|
|
|
|
2023-05-29 23:09:21 +08:00
|
|
|
// Output the decimal equivalent
|
|
|
|
printf("Decimal equivalent: %d\n", decimal_number);
|
|
|
|
|
|
|
|
return 0;
|
2017-07-13 07:59:31 +08:00
|
|
|
}
|