Home / Programs / Program to raise a floating point number to a positive integer by the iterative and recursive method
🚀 Programming Example

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

👁 1,970 Views
💻 Practical Program
📘 Step Learning
Program to raise a floating point number to a positive integer

💻 Program Code

#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()*/
                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.