Programming Example
Create a power function for negative exponent using c
Here in this program no need to put the negative sign of n
#include<stdio.h>
void power(float, float);
void main()
{
float x, n;
printf("Enter The value of x and n : ");
scanf("%f%f",&x,&n);
power(x,n);
}
void power(float x, float n)
{
int i;
float result = 1;
for(i= 0; i<n; i++)
{
result = (result*x);
}
result = 1/result;
printf("%.1f^-%.1f = %f",x,n,result);
}
Enter The value of x and n : 2 2
2.0^-2.0 = 0.250000Press any key to continue . . .
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.