#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... */
Enter the values of x and n
x = ? 2
n = ? 3
Value of x^n is 8
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.