Problem Statement
Write a c program to convert binary number to decimal number
you should take the input by getchar() function
#include<stdio.h>
int twopow(int j);
int main()
{
int i=0,b[50],j,sum;
printf("enter the number in binary ");
do
{
b[i]=getchar();
i++;
}
while(b[i-1]!='\n');
sum=0;
for(j=0;j<i-1;j++)
{
printf("%d ",b[j]);
sum=sum+(b[j]-48)*twopow(i-2-j); // getchar stores the ansii code of numbers so b[j]-48
}
printf("the equivalent decimal number is %d \n",sum);
return 0;
}
int twopow(int j)
{
int mul,i;
mul=1;
for(i=0;i<j;i++)
{
mul=mul*2;
}
return mul;
}
Write a c program to convert binary number to decimal number
you should take the input by getchar() function
#include<stdio.h>
int twopow(int j);
int main()
{
int i=0,b[50],j,sum;
printf("enter the number in binary ");
do
{
b[i]=getchar();
i++;
}
while(b[i-1]!='\n');
sum=0;
for(j=0;j<i-1;j++)
{
printf("%d ",b[j]);
sum=sum+(b[j]-48)*twopow(i-2-j); // getchar stores the ansii code of numbers so b[j]-48
}
printf("the equivalent decimal number is %d \n",sum);
return 0;
}
int twopow(int j)
{
int mul,i;
mul=1;
for(i=0;i<j;i++)
{
mul=mul*2;
}
return mul;
}
No comments:
Post a Comment