ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# C 程序:将十进制数转换为八进制数 > 原文: [https://beginnersbook.com/2017/09/c-program-to-convert-decimal-to-octal-number/](https://beginnersbook.com/2017/09/c-program-to-convert-decimal-to-octal-number/) 该程序将输入的十进制数转换为八进制数。 ## 示例:将十进制转换为八进制的程序 在这个程序中,我们为十进制到八进制转换创建了一个用户定义的函数`decimalToOctal()`。程序将十进制数(由用户输入)作为输入,并使用该函数将其转换为八进制数。要理解这个程序,您应该熟悉以下 [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> /* This function converts the decimal number "decimalnum" * to the equivalent octal number */ int decimalToOctal(int decimalnum) { int octalnum = 0, temp = 1; while (decimalnum != 0) { octalnum = octalnum + (decimalnum % 8) * temp; decimalnum = decimalnum / 8; temp = temp * 10; } return octalnum; } int main() { int decimalnum; printf("Enter a Decimal Number: "); scanf("%d", &decimalnum); printf("Equivalent Octal Number: %d", decimalToOctal(decimalnum)); return 0; } ``` 输出: ```c Enter a Decimal Number: 436 Equivalent Octal Number: 664 ``` 看看这些相关的 C 程序: 1. [C 程序:将八进制转换为十进制](https://beginnersbook.com/2017/09/c-program-to-convert-octal-number-to-decimal-number/) 2. [C 程序:将八进制转换为二进制](https://beginnersbook.com/2017/09/c-program-to-convert-octal-number-to-binary-number/) 3. [C 程序:将二进制转换为八进制](https://beginnersbook.com/2017/09/c-program-to-convert-binary-to-octal-number-system/) 4. [C 程序:将二进制转换为十进制](https://beginnersbook.com/2015/02/c-program-to-convert-binary-number-to-decimal-number/) 5. [C 程序:将大写字符串转换为小写字符串](https://beginnersbook.com/2015/02/c-program-to-convert-uppercase-string-to-lowercase-string/)