1、定义一个函数,用于计算10的幂次方
#include <stdio.h> double power_of_ten(int n) { return pow(10, n); }
2、使用main
函数调用power_of_ten
函数,并输出结果
#include <stdio.h> #include <math.h> double power_of_ten(int n) { return pow(10, n); } int main() { int n; printf("请输入一个整数:"); scanf("%d", &n); double result = power_of_ten(n); printf("10的%d次方等于:%lf ", n, result); return 0; }
3、编译和运行程序
在命令行中,使用gcc
编译器编译代码:
gcc o power_of_ten power_of_ten.c lm
lm
表示链接数学库。
4、运行程序
./power_of_ten
输入一个整数,程序将输出10的该整数次方的结果。