#include <stdio.h>
#include <math.h>
void main()
{
long int x,n,xpown;
long int power(int x, int n);
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”);
}
/*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)
return (pow(power(x,n/2),2)); /*if n is even*/
else
return (x*power(x, n-1)); /* if n is odd*/
}
/*————————————-
Output
Enter the values of X and N
2 5
X to the power N = 32
RUN2
Enter the values offX and N
4 4
X to the power N ==256
RUN3
Enter the values of X and N
5 2
X to the power N = 25
RUN4
Enter the values of X and N
10 5
X to the power N = 100000
—————————————–*/
Not Satisfied ? Just search & get the result
Related posts:
- C program to find the biggest of three numbers
- C program to compute the surface area and volume of a cube
- C program to evaluate the given polynomial P(x)=AnXn + An-1Xn-1 + An-2Xn-2+… +A1X + A0, by reading its coefficients into an array. [Hint:Rewrite the polynomial as P(x) = a0 + x(a1+x(a2+x(a3+x(a4+x(...x(an-1+xan)))) and evaluate the function starting from the inner loop]
- C program to find the area of a triangle, given three sides
- Write a C program to find the simple interest , given principle,rate of interest and times
