img
Question:
Published on: 3 December, 2024

Write a C program to convert binary to decimal using function.

Answer:

 

#include<stdio.h>
#include<conio.h>
#include<math.h>
int bin_to_dec(int);
main()
{
	int n,result;
	printf("\n Enter any binary number");
	scanf("%d",&n);
	result=bin_to_dec(n);
	printf("\n The equivalant decimal=%d",result);
	getch();
}
int bin_to_dec(int x)
{
	int i=0,digit,sum=0;
	do
	{
		digit=x%10;
		sum=sum+digit*pow(2,i);
		x=x/10;
		i++;
	}while(x!=0);
	return(sum);
}

 

Output:

Random questions