Spread the love

Question :- C Program to convert Binary to Decimal

This C Program Converts the given Binary number to Decimal. Binary number is a number that can be represented using only two numeric symbols – 0s and 1s so which is a number in base 2. Decimal is base 10 arithmetic where each digit is a value from 0 to 9.

Algorithm

BINARY TO DECIMAL

C Program to convert Binary to Decimal

 

#include <stdio.h>
 
/* Driver function */ 
int BinaryToDecimal(long int binary) 
{
	long int decimal = 0, i = 1, remainder;
	
	/* Iterate unitl number becomes zero */
	while (binary != 0)
    {
        remainder = binary % 10;
        decimal = decimal + remainder * i;
        i = i * 2;
        binary = binary / 10;
    }
    
    return decimal;
}
	
/* Main Method */
int main()
{
    long int binary = 10011011;
    
    printf("Equivalent Decimal value:     %ld \n\n", BinaryToDecimal(binary));
     
    printf("Equivalent hexadecimal value: %lX \n\n", BinaryToDecimal(binary));
    
	return 0;
}

 

Suggested Reading

  1. Convert decimal number into hexadecimal octal binary – single universal logic
  2. Binary representation of a given number
  3. Write a c Program to convert decimal to Hexadecimal number