Write a C program to Convert the given Binary Number into Decimal
Program:-
#include <stdio.h>
#include <math.h>
int binary_to_decimal(long int n)
{
int decimal = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimal += remainder*pow(2,i);
++i;
}
return decimal;
}
int main()
{
long int n;
printf(“Enter a binary number: “);
scanf(“%ld”, &n);
printf(“\nDecimal number : %d\n “, binary_to_decimal(n));
return 0;
}
#include <math.h>
int binary_to_decimal(long int n)
{
int decimal = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimal += remainder*pow(2,i);
++i;
}
return decimal;
}
int main()
{
long int n;
printf(“Enter a binary number: “);
scanf(“%ld”, &n);
printf(“\nDecimal number : %d\n “, binary_to_decimal(n));
return 0;
}
Output:-
....
0 comments:
Post a Comment