Home / Programs / Create a power function for negative exponent using c
Programming Example

Create a power function for negative exponent using c

👁 5,271 Views
💻 Practical Program
📘 Step by Step Learning
Here in this program no need to put the negative sign of n

Program Code

#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);
 
}

Output

Enter The value of x and n : 2  2
2.0^-2.0 = 0.250000Press any key to continue . . .

Explanation

Here in this program no need to put the negative sign of n

How to learn from this program

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.