Create a power function for negative exponent using c

C Programming Language Function in C Language (Article) Function in C Language (Program)

5195

Program:

#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

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.