C program to compute the value of X ^ N given X and N as inputs

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C program to compute the value of X ^ N given X and N as inputs */

#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 Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to find the biggest of three numbers
  2. C program to compute the surface area and volume of a cube
  3. 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]
  4. C program to find the area of a triangle, given three sides
  5. Write a C program to find the simple interest , given principle,rate of interest and times

Leave a Comment

Previous post:

Next post: