Write a program in c using recursion to calculate the power of an inter number. The number and power will be entered by user.
In this program we shall learn through a program that how to calculate power of an integer number using recursion in c programming language. In this program ask for a number and power to be calculated to the user, and display the corresponding value of the power of number.
Example:
Enter number = 5
Enter power = 3
Calculated value = 125
Now see the program:
#include<stdio.h>
#include<conio.h>
int Power(int,int);
int main()
{
int number,power,result;
printf("Enter number\t");
scanf("%d",&number);
printf("Enter the power to be calculated\t");
scanf("%d",&power);
result = Power(number,power);
printf("The result of %d to the power %d is = %d\n",number,power,result);
}
int Power(int a,int b)
{
static int r = 1;
if(b == 0)
{
return(1);
}
else
{
r = r*a;
Power(a,b-1);
}
return(r);
}
Output:
Enter number = 6Enter the power to be calculated = 2
The result of 6 to the power 2 is = 36
Do not forget to subscribe me for latest update on my blog , follow me and grow your knowledge. thank you
0 comments:
Post a Comment