beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

C Program to Convert Binary Number to Decimal Number

By Chaitanya Singh | Filed Under: C Programs

This program converts binary number to equivalent decimal number.

Example: Program to convert binary to decimal

In this program, we have created a user defined function binaryToDecimal() for binary to decimal conversion. This programs takes the binary number (entered by user) as input and converts it into a decimal number using function. To understand this program, you should be familiar with the following C programming concepts:

  1. User defined function in C
  2. C Programming while loop
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
    int decimalnum = 0, temp = 0, remainder;
    while (binarynum!=0)
    {
        remainder = binarynum % 10;
        binarynum = binarynum / 10;
        decimalnum = decimalnum + remainder*pow(2,temp);
        temp++;
    }
    return decimalnum;
}

int main()
{
    long binarynum;
    printf("Enter a binary number: ");
    scanf("%ld", &binarynum);

    printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));
    return 0;
}

Output:

Enter a binary number: 1010111
Equivalent decimal number is: 87

Check out these related C Programs:

  1. C Program to convert Decimal number to Octal
  2. C Program to convert Octal number to decimal
  3. C Program to convert Octal number to Binary
  4. C Program to convert Binary number to Octal
  5. C Program to convert lowercase string to uppercase

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap