C Program to Convert Binary Number to Decimal

C Program to Convert Binary Number to Decimal

Senior Developer Asked on February 11, 2017 in C.
Add Comment
1 Answer(s)

#include <stdio.h>

void main()

{

int  num,rem,bin_val, dec_val = 0, base = 1;
printf("Enter a binary number(1s and 0s) \n");

scanf("%d", &num);

bin_val = num;

while (num > 0)

{

rem = num % 10;

dec_val = dec_val + rem * base;

num = num / 10 ;

base = base * 2;
}
printf("The Binary number is = %d \n", bin_val);

printf("Decimal equivalent is = %d \n", dec_val);
}

Senior Developer Answered on February 22, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.