Saturday, November 10, 2012

User Define Function- Power

Q. Write a C program to create function power and calculate the power of any number.
For example:
Assume number = 5
Power = 3
Result = 5*5*5 = 125

Ans.

/*c program for creating user define function power and calculating power of number*/
#include<stdio.h>
int power(int , int );
int main()
{
 int num,pow,res;   //res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 printf("Enter power of number : ");
 scanf("%d", &pow);
 res = power(num,pow);
 printf("%d's power %d = %d",num,pow,res);
 return 0;
}

int power(int n, int p)
{
 int r=1;
 for(; p>=1; p--)
    r = r*n;
 return r;
}

The output of above program would be:


Output of user define function "power" C program
Figure: Screen shot for calculating power of any number
using user define function power C program

No comments:

Post a Comment