0% found this document useful (0 votes)
13 views1 page

Calculate Power of a Number in C

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Calculate Power of a Number in C

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>

// function to calculate x raised to the power n


long long power(int x, int n) {
// base case
if (n == 0)
return 1;
else if (n < 0)
return power(x, -n) / x;
else
// recursive case
return x * power(x, n-1);
}

int main() {
int x, n;
printf("Enter a number: ");
scanf("%d", &x);
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("%lld", power(x, n));
return 0;
}

You might also like