原文: https://beginnersbook.com/2017/09/c-program-to-convert-octal-number-to-decimal-number/
该程序将八进制数转换为等效的十进制数。
示例:将八进制转换为十进制的程序
在这个程序中,我们为八进制到十进制转换创建了一个用户定义的函数。程序将八进制数(由用户输入)作为输入,并使用函数将其转换为十进制数。要了解此程序,您应该具有以下 C 编程主题的知识:
#include <stdio.h>#include <math.h>/* This function converts the octal number "octalnum" to the* decimal number and returns it.*/long octalToDecimal(int octalnum){int decimalnum = 0, temp = 0;while(octalnum != 0){decimalnum = decimalnum + (octalnum%10) * pow(8,temp);temp++;octalnum = octalnum / 10;}return decimalnum;}int main(){int octalnum;printf("Enter an octal number: ");scanf("%d", &octalnum);printf("Equivalent decimal number is: %ld", octalToDecimal(octalnum));return 0;}
输出:
Enter an octal number: 754Equivalent decimal number is: 492
看看这些相关的 C 程序:
