Program to raise a floating point number to a positive integer by the iterative and recursive method

C Programming Language / Recursion in c

1986

Program:

#include<stdio.h>
float power(float a , int n);
float Ipower(float a , int n);
main( )
{
float a, p;
int n;
printf("Enter a and n : ");
scanf("%f %d", &a, &n);
p = power(a, n);
printf("%f raised to power %d is %f\n", a, n, p);
p = Ipower(a, n);
printf("%f raised to power %d is %f\n", a, n, p);
}/*End of main()*/

/*Recursive*/
float power(float a , int n)
{
if(n == 0)
return(1);
else
return(a * power(a,n-1));
}/*End of power()*/

/*Iterative*/
float Ipower(float a , int n)
{
int i;
float result=1;
for(i=1; i<=n; i++)
result = result * a;
return result;
}/*End of Ipower()*/

Output:

Enter a and n : 1.2
2
1.200000 raised to power 2 is 1.440000
1.200000 raised to power 2 is 1.440000
Press any key to continue . . .

Explanation:

None

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.