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:
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:
Figure: Screen shot for calculating power of any number using user define function power C program |
Hey bro my name is Neeraj and i have blog which is related to c++ programming. I want to exchange link between us. If you are interested than let me know. My blog url is cpp-programs.blogspot.com and my email is sareneeru94@gamil.com
ReplyDeleteBt this will work only for integer powers... What about floats and negative powers?
ReplyDelete