C Program for User Defined Power Function

Write a c program to implement a user defined power function. A power function takes two parameters x and y and calculates xy.

C Program for User Defined Power Function

C Program

/* Aim: Write a function for the following function prototype
int power(int x, int y) */ 

#include<stdio.h>

int power(int x, int y); // Function Prototype

void main()
{
	int x,y;
	printf("\n Enter any value and power:- ");
	scanf("%d%d",&x,&y);

&#x9:printf("\n The value of %d^%d is %d \n \n",x,y,power(x,y));
}

// Power Function

int power(int x, int y)
{
&#x9:int i,prod=1;
 
&#x9:for(i=1;i<=y;i++)
&#x9:{
&#x9:prod*=x;
&#x9:} 

&#x9:return prod;

} // End of the code 

Output

/* Output of above code:-
 Enter any value and power:- 4 2

 The value of 4^2 is 16 */ 

Comments