Exercício resolvido - Função POINT que recebe um...

12/10/2013 14:32

Função POINT que recebe um parâmetros inteiro n e x real e calcula x elevado a n (n pode ser < 0).

Solução:

#include <stdio.h>
#include <stdlib.h>
float POINT(float x, int n)
{  
    float pot = 1;
    int i;  
    if(n == 0)
    {
        return(pot);
    }
    if(n > 0)
    {
         for(i = 1; i <= n; i++)
         {
              pot = x * pot;
         }
         return(pot);
    }
    if(n < 0)
    {
         n = -1 * n;
         for(i = 1; i <= n; i++)
         {
              pot = x * pot;
         }
         pot = 1/pot;
         return(pot);
    }
}
main()
{
       int expoente;
       float base;
       printf("Calcular potencia:\n\n");
       printf("Base = ");
       scanf("%f", &base);
       printf("Expoente = ");
       scanf("%d", &expoente);
       printf("\n%.2f elevado a %d eh: %.4f\n\n", base, expoente, POINT(base, expoente));     
       system("pause");
}