Power function returning incorrect values inside the while loop

1

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;
}
    
asked by anonymous 05.05.2018 / 17:11

1 answer

3

The main problem is the wrong scan() formatting, and it has nothing to do with tie. d is for integers, and double must use lf .

#include <stdio.h>

double power(double n, int p) {
    double pow = 1;
    for (int i = 1; i <= p; i++) pow *= n;
    return pow;
}

int main() {
    printf("%.0f\n", power(5, 2));
    printf("Enter n and p (n^p): ");
    double n;
    int p;
    scanf("%lf", &n);
    scanf("%d", &p);
    printf("The pow is: %.0f\n", power(n, p));
}
    
05.05.2018 / 17:59