I have a function power
that returns the value of a potentiation by receiving the parameters n
and p
( n^p
). Out of the loop while
the function returns the correct values, however inside the loop the function returns incorrect values,
double power(double, int);
int main()
{
double n;
int p;
printf("%.0f\n", power(5, 2)); // aqui retorna o valor correto de 5^2
while (1)
{
printf("Enter n and p (n^p): ");
scanf("%.0f", &p);
scanf("%d", &n);
printf("The pow is: %.0f\n", power(n, p)); // aqui retorna valores incorretos
}
return 0;
}
double power(double n, int p)
{
double pow = 1;
int i;
for (i = 1; i <= p; i++)
pow *= n;
return pow;
}