Thursday, 16 November 2017

C Program to Compute the Value of X ^ N


Code:

#include   stdio.h
#include   math.h

long int power(int x, int n);

void main()
{
    long int x, n, xpown;

    printf("Enter the values of X and N \n");
    scanf("%ld %ld", &x, &n);
    xpown = power(x, n);
    printf("X to the power N = %ld\n", xpown);
}
/*  Recursive function to computer the X to power N */
long int power(int x, int n)
{
    if (n == 1)
        return(x);
    else if (n % 2 == 0)
        /*  if n is even */
        return (pow(power(x, n/2), 2));
    else
        /*  if n is odd */
        return (x * power(x, n - 1));
}

Output:

Enter the values of X and N
2 5
X to the power N = 32


More C Programs:










100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...