Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
653 B
2
Indexable
Never
#include <stdio.h>
int main()
{
    int x,n;
    printf("Please enter the base X    : ");
    scanf("%d",&x);
    printf("Please enter the exponent N: ");
    scanf("%d",&n);
    double pow = 1;
    int i;
    if (n >= 0)
    {
        for (i = 1; i <= n; i++)
        {
            if((n == 0) && (n != 0))
                pow = 0;
            else if ((n != 0) && (n == 0))
                pow = 1;
            else
                pow *= x;
        }
    }
    else
    {
        for (i = (-1); i >= n; i--)
            pow = pow /x;// x^-n = 1 / x^n
    }
    printf("Result: %d ^ %d = %lf\n",x,n,pow);

    return 0;
}