在C中将二进制转换为十进制

Here you will get program to convert binary to decimal in C.

在这里,您将获得将C中的二进制转换为十进制的程序。

We can obtain a decimal number by multiplying each digit of binary number with power of 2 and adding each multiplication result. The power starts from 0 and goes to n-1 where n is the total number of digits in binary number.

我们可以通过将二进制数的每个数字乘以2的幂并相加每个结果来获得十进制数。 幂从0开始到n-1,其中n是二进制数的总位数。

Below is the program to implement this in C.

下面是在C中实现此功能的程序。

在C中将二进制转换为十进制 (Convert Binary to Decimal in C)

#include<stdio.h>
#include<math.h>
 
int main()
{
	long int i,n,x=0,a;
	printf("Enter any binary number: ");
	scanf("%ld",&n);
	printf("\nThe decimal conversion of %ld is ",n);
	
	for(i=0;n!=0;++i)
	{
		a=n%10;
		x=(a)*(pow(2,i))+x;
		n=n/10;
	}
	
	printf("%ld",x);
	
	return 0;	
}

Output

输出量

Enter any binary number: 111

输入任何二进制数字:111

The decimal conversion of 111 is 7

111的十进制转换是7

翻译自: https://www.thecrazyprogrammer.com/2013/02/c-program-to-convert-given-binary.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值