企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# C 程序:将二进制数转换为十进制数 > 原文: [https://beginnersbook.com/2015/02/c-program-to-convert-binary-number-to-decimal-number/](https://beginnersbook.com/2015/02/c-program-to-convert-binary-number-to-decimal-number/) 该程序将二进制数转换为等效的十进制数。 ## 示例:将二进制转换为十进制的程序 在这个程序中,我们创建了一个用户定义的函数`binaryToDecimal()`,用于二进制到十进制的转换。该程序将二进制数(由用户输入)作为输入,并使用函数将其转换为十进制数。要理解这个程序,您应该熟悉以下 [C 编程](https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/)的概念: 1. [C 中的用户定义函数](https://beginnersbook.com/2014/01/c-functions-examples/) 2. [C `while`循环(https://beginnersbook.com/2014/01/c-while-loop/) ```c #include <stdio.h> #include <math.h> int binaryToDecimal(long binarynum) { int decimalnum = 0, temp = 0, remainder; while (binarynum!=0) { remainder = binarynum % 10; binarynum = binarynum / 10; decimalnum = decimalnum + remainder*pow(2,temp); temp++; } return decimalnum; } int main() { long binarynum; printf("Enter a binary number: "); scanf("%ld", &binarynum); printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum)); return 0; } ``` 输出: ```c Enter a binary number: 1010111 Equivalent decimal number is: 87 ``` 看看这些相关的 C 程序: 1. [C 程序:将十进制数转换为八进制](https://beginnersbook.com/2017/09/c-program-to-convert-decimal-to-octal-number/) 2. [C 程序:将八进制数转换为十进制数](https://beginnersbook.com/2017/09/c-program-to-convert-octal-number-to-decimal-number/) 3. [C 程序:将八进制数转换为二进制数](https://beginnersbook.com/2017/09/c-program-to-convert-octal-number-to-binary-number/) 4. [C 程序:将二进制数转换为八进制](https://beginnersbook.com/2017/09/c-program-to-convert-binary-to-octal-number-system/) 5. [C 程序:将小写字符串转换为大写](https://beginnersbook.com/2015/02/c-program-to-convert-lowercase-string-to-uppercase-string/)