C语言练习:使用递归函数计算出一个数的幂次方
#include <stdio.h> int power(int n1, int n2); int main() { int base, a, result; printf("Enter base number: "); scanf("%d", &base); printf("Enter power number(positive integer): "); scanf("%d", &a); result = power(base, a); printf("%d^%d = %d", base, a, result); return 0; } int power(int base, int a) { if (a != 0) return (base * power(base, a - 1)); else return 1; }
结果
Enter base number: 3
Enter power number(positive integer): 4
3^4 = 81
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!