Write a function that computes xn, where x is any valid number and n an integer value.

C Programming Language / Function in C Language

1750

Program:

#include <stdio.h>
#include <math.h>

double power(double , int);

int main(void)
{
	double x, result;
	int n;
	printf("\n Enter the values of x and n \n");
	printf("\n x = ?  ");
	scanf("%lf",&x);
	printf("\n n = ?  ");
	scanf("%d",&n);
	result=power(x,n);
	printf("\n Value of x^n is %g",result);
	return 0;

}


/**********************************************************************/
/* Function to compute integral powers of any valid number.           */ 
/* First argument is any valid number, second argument is power index.*/
/**********************************************************************/

double power(double x, int n)     /* function header */
{                                 /* function body starts here...   */
   double result = 1.0;  
   int i;         /* declaration of variable result */
   for(i = 1; i<=n; i++)      /* computing xn     */
          result *= x;                /*       :         */
   return result;       /* return value in 'result' to calling function*/
}                                 /* function body ends here...     */

Output:

 Enter the values of x and n

 x = ?  2

 n = ?  3

 Value of x^n is 8
 

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.