C Online Compiler
Example: Binary to Decimal Converter in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Binary to Decimal Converter #include <stdio.h> #include <math.h> // For pow() function, though can be avoided with a variable int main() { long long binaryNum; int decimalNum = 0, i = 0, remainder; // Step 1: Prompt user for input printf("Enter a binary number: "); scanf("%lld", &binaryNum); // Step 2: Iterate through the binary number while (binaryNum != 0) { remainder = binaryNum % 10; // Get the last digit binaryNum /= 10; // Remove the last digit // Step 3: Add to decimal equivalent // If the digit is 1, add 2^i to decimalNum decimalNum += remainder * pow(2, i); i++; // Increment the power of 2 for the next digit } // Step 4: Display the result printf("Decimal equivalent: %d\n", decimalNum); return 0; }
Output
Clear
ADVERTISEMENTS