Banner Header

Thursday, 7 September 2017

C Program to Convert Binary Number to Decimal

C Program to Convert Binary Number to Decimal

In this example, you will learn to convert binary number to decimal and decimal number to binary manually by creating a user-defined function.



Example 1: Program to convert binary number to decimal

#include <stdio.h>
#include <conio.h>
#include <math.h>
int convertBinaryToDecimal(long long n);
int main()
{
    long long n;
    printf("Enter a binary number: ");
    scanf("%lld", &n);
    printf("%lld in binary = %d in decimal", n, convertBinaryToDecimal(n));
    return 0;
}
int convertBinaryToDecimal(long long n)
{
    int decimalNumber = 0, i = 0, remainder;
    while (n!=0)
    {
        remainder = n%10;
        n /= 10;
        decimalNumber += remainder*pow(2,i);
        ++i;
    }
    return decimalNumber;
    getch();
}
Output
Enter a binary number: 110110111
110110111 in binary = 439

Click on the download button to download file

download button
C Program to Convert Binary Number to Decimal Reviewed by Waqas Ahmed Mir PC on September 07, 2017 Rating: 5 C Program to Convert Binary Number to Decimal In this example, you will learn to convert binary number to decimal and decimal number to bina...

No comments: