计算一个数的整数幂
#include<iostream>
#include<string.h>
#include<limits.h>
#include<float.h>
#include<stdio.h>
#pragma warning(disable : 4996)
using namespace std;
double power(double n, int p);
int main() {
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf("to which\nthe number will be rasised.Enter q");
printf("to quit.\n");
while (scanf("%lf%d",&x,&exp)==2)
{
xpow = power(x, exp);
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip --bye!\n");
return 0;
}
double power(double n, int p) {
double pow = 1;
for (int i = 0; i < p; i++)
{
pow *= n;
}
return pow;
}

浙公网安备 33010602011771号