C Program to Convert Binary to Decimal Number Using User Defined Function

#include<stdio.h>

/* Function Prototype */
long int binaryToDecimal(long int num);

/* Main Function */
int main()
{
	 long int binary, decimal;
	 printf("Enter valid binary number: ");
	 scanf("%ld", &binary);
	 decimal = binaryToDecimal(binary);
	 printf("Binary (%ld) = Decimal (%ld)", binary, decimal);
	 return(0);
}

/* Function Definition */
long int binaryToDecimal(long int num)
{
    long int decimal=0, base=1, rem;
    
    while(num!=0)
    {
        rem = num%10;
        decimal = decimal + base*rem;
        base = base * 2;
        num = num/10;
    }
    return decimal;
}

Output

Enter valid binary number: 1011010111
Binary (1011010111) = Decimal (727)

Leave a Reply

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

https://atozonlinesolutions.com/wp-content/uploads/2017/03/logo_white.png
https://atozonlinesolutions.com/wp-content/uploads/2017/03/logo_white.png
About
Products
Subscribe

If you wish to receive our latest news in your email box, just subscribe to our newsletter. We won’t spam you, we promise!

    Applauz

    As the pioneer of the lean startup movement, APPLAUZ has dedicated it’s time to sharing effective business strategies that help new businesses and enterpreneurs put their money to work in the right way.

    Subscribe

    If you wish to receive our latest news in your email box, just subscribe to our newsletter. We won’t spam you, we promise!

      Applauz

      As the pioneer of the lean startup movement, APPLAUZ has dedicated it’s time to sharing effective business strategies that help new businesses and enterpreneurs put their money to work in the right way.

      Copyright by BOLD THEMES. All rights reserved.