Home / Programs / Create a power function using c
Programming Example

Create a power function using c

👁 973 Views
💻 Practical Program
📘 Step by Step Learning
Create a power function using c

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);
 }  
 
 printf("%.1f^%.1f = %f",x,n,result);
 
}

Output

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

Explanation

None

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.