[英]C Program for converting binary number to decimal isn't working
Following is a C program which i wrote for converting binary number into decimal, but it always gives output '0'.以下是我编写的用于将二进制数转换为十进制数的 C 程序,但它总是给出 output '0'。 Can someone point out the mistake?有人可以指出错误吗?
#include<stdio.h>
main()
{
int a=1/2,p=0,rem,n;
printf("Enter a binary number:");
scanf("%d",&n);
while(n!=0)
{
a=a*2;
rem=n%10;
p=p+rem*a;
n=n/10;
}
printf("Its decimal equivalent is %d",p);
}
when you divide integer numbers in c its round the solution.当您在 c 中划分 integer 数字时,它会围绕解决方案。 so if you want to calc a/b as float you need casting like this a/(float)b
因此,如果您想将 a/b 计算为浮点数,则需要像这样进行转换a/(float)b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.