Convert Binary Number to Decimal and Vice-Versa in C

Exercise:

Write a C Program to convert binary number to decimal and vice-versa.

1. Convert Binary to Decimal

Click Here to View the Solution!
#include <math.h>
#include <stdio.h>
int BiToDec(long long num);

int main() {
    long long num;
    printf("Enter a binary number: ");
    scanf("%lld", &num);
    printf("%lld in binary = %d in decimal", num, BiToDec(num));
    return 0;
}

int BiToDec (long long num) {
    int dec = 0, i = 0, rem;
    while (num != 0) {
        rem = num % 10;
        num /= 10;
        dec += rem * pow(2, i);
        ++i;
    }
    return dec;
}
Click Here to View the Output!
Enter a binary number: 10110001
10110001 in binary = 177 in decimal
Click Here to View the Explanation!
  • This program is used to convert a binary number into a decimal.
  • An integer type user-defined function convert is declared with the parameter long long num.
  • In main(), the program requests the user to enter a binary number and stores it in the variable num using the scanf() function.
  • Initially, the convert function is called from main with the entered integer as its parameter.
  • The convert function is then initialized using a while loop which will continue its iterations until n is not equal to 0.
  • The decimal form of the binary number is calculated using the remainder of the number by taking its modulus with 10.
  • The number will be reduced 10 times by dividing by 10 to remove the last digit of num.
  • For the decimal number, remainder is multiplied with pow(2 , i) where i is initially equal to 0.
  • The result will be added to the variable dec and i will be incremented in each iteration until n becomes equal to 0.
  • dec will then be returned to the main for printing.

2. Convert Decimal to Binary

Click Here to View the Solution!
#include <math.h>
#include <stdio.h>
long long DecToBi(int num);

int main() {
    int num;
    printf("Enter a decimal number: ");
    scanf("%d", &num);
    printf("%d in decimal = %lld in binary", num, DecToBi(num));
    return 0;
}

long long DecToBi(int num) {
    long long bin = 0;
    int rem, i = 1, step = 1;
    while (num != 0) {
        rem = num % 2;
        printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, num, rem, num / 2);
        num /= 2;
        bin += rem * i;
        i *= 10;
    }
    return bin;
}
Click Here to View the Output!
Enter a decimal number: 12 
Step 1: 12/2, Remainder = 0, Quotient = 6 
Step 2: 6/2, Remainder = 0, Quotient = 3 
Step 3: 3/2, Remainder = 1, Quotient = 1 
Step 4: 1/2, Remainder = 1, Quotient = 0 
12 in decimal = 1100 in binary 
Click Here to View the Explanation!
  • This program is used to convert a decimal number to a binary number.
  • An integer type user-defined function convert is declared with the parameter long long num.
  • In main(), the program requests the user to enter a decimal number and stores it in the variable num using the scanf() function.
  • Initially, the convert function is called from main with the entered integer as its parameter.
  • The convert function is then initialized using a while loop which will continue its iterations until num is not equal to 0.
  • The remainder of the number is used for calculating the binary form through the formula num % 2.
  • All the steps of the binary number calculation are printed along with the remainder and quotient.
  • The value of n is updated by dividing n by 2 for the next iteration.
  • For the binary number, remainder is multiplied with i which was initially set as 1 and added to the variable bin.
  • In each iteration i will be multiplied with 10 to update the value of i and to be used in the next iteration.
  • The final value in bin is then returned to main for printing.

%d bloggers like this: