Program To Find Power of a number using Function - C Program
Program To Calculate Power Using Function
#include <stdio.h>
double power(int a, int b);
int main()
{
int a, b;
double pnum = 1;
printf("Enter the value of A :");
scanf("%d",&a);
printf("Enter the value of B :");
scanf("%d",&b);
pnum=power(a,b);
printf("%d Raised to the Power of %d is %lf", a, b, pnum);
return 0;
}
double power(int a, int b)
{
double pnum=1;
int i;
if (b > 0)
{
for (i = b; i>0; i--)
pnum = pnum * a;
}
else if(b < 0)
{
for(i=b; i<0; i++)
pnum=pnum/a;
}
return pnum;
}
Output - Program To Calculate Power Using Function
Enter the value of A :5
Enter the value of B :3
5 Raised to the Power of 3 is 125.000000