Spread the love

Write a C program to calculate power(x,n)

Logic is very simple which include recursive call of same function until power become zero and multiply the results as you can see below.

 

#include<stdio.h>
 
unsigned int power(unsigned int x, unsigned int n)
{
    if( n == 0)
        return 1;

    return x*power(x, --n);
}

int main()
{
    unsigned int x = 2;
    unsigned int n = 3;
 
    printf("%d", power(x, n));

    return 0;
}

 

Output

Write a c program power(x,n)