Programs Using Functions - C Programming
Write a user defined function that computes 'x' raised to the power of 'y'.
The problem 'x' raised to the power of 'y' means xy, for this the input values are 'x' and 'y'.
Example: 102 Means 10*10 Evaluates 100
/* C Programs to computes 'x' raised to the power of 'y' */
#include <stdio.h>
#include <conio.h>
main()
{
int x,y;
int power(int, int);
clrscr();
printf("Enter x and y values....");
scanf("%d %d", &x, &y);
printf("%d to the power of %d is.. %f",x,y,power(x,y));
} /* main */
int power(int x, int y) /* Power is a function with arguments */
{
float p=1.0;
if(y >=0)
while(y‒‒)
p*=x;
else
while(y++)
p/=x;
return(p);
} /* power */
OUTPUT
Enter x and y values.... 10 2
10 to the power of 2 is.. 100
EXPLANATION: The above program calculates the value of 'x' to the power of y using the user defined function power(), which has two parameters of the type int and these can be taken from the input device.
Computer Programming C Laboratory: Functions C Programs : Tag: C Programming : - C Programs to computes x raised to the power of y