Oct 3, 2011

C Program To Compute X ^ N

C program to compute the value of X ^ N, given X and N as inputs. Program accepts two integers x, n and computes the power of x to the n by using recursion function power().  Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                        Happy Coding
***********************************************************/


#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(" %d to the power %d = %ld\n",x,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)
  return (pow(power(x,n/2),2));  /*if n is even*/
 else
  return (x*power(x, n-1));     /* if n is odd*/
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!