C++ProgrammingServer Side Programming The power of a number can be calculated as x^y where x is the number and y is its power. For example. Let’s say, x = 2 and y = 10 x^y =1024 Here, x^y is 2^10A program to find the power using recursion is as follows. ExampleLive Demo #include <iostream> using namespace std; int FindPower(int base, int power) { if (power == 0) return 1; else return (base * FindPower(base, power-1)); } int main() { int base = 3, power = 5; cout<<base<<" raised to the power "<<power<<" is "<<FindPower(base, power); return 0; }Output3 raised to the power 5 is 243In the above program, the function findPower() is a recursive function. If the power is zero, then the function returns 1 because any number raised to power 0 is 1. If the power is not 0, then the function recursively calls itself. This is demonstrated using the following code snippet. int FindPower(int base, int power) { if (power == 0) return 1; else return (base * findPower(base, power-1)); }In the main() function, the findPower() function is called initially and the power of a number is displayed. This can be seen in the below code snippet. 3 raised to the power 5 is 243
Updated on 24-Jun-2020 08:06:18
In this article, we will discuss the concept of Calculate power of a number using recursion in C++ programming language In this post, we will learn how to find the power of a number using a recursive function in C language Program 1 #include <iostream> #include <conio.h> using namespace std; int find_Power(int num1,int num2); int main() { int base, powerValue, result; cout<<"Enter base number: "; cin>>base; cout<<"Enter power number: "; cin>>powerValue; result=find_Power(base,powerValue); cout<<base<<"^"<<powerValue<<" is "<<result; getch(); return 0; } int find_Power(int base, int powerValue) { if(powerValue !=0) return (base*find_Power(base,powerValue-1)); else return 1; }When the above code is executed, it produces the following results In the above program, the function find_Power() is a recursive function. When the power is not equal to 0 the function recursively call it self to calculate power When the power is equal to 0 the function return 1 – any number raised to the power of 0 is 1 you want to find power of any number, you can use pow() function in C++ language Similar post find the power of a number in C using recursion find the power of a number in Python using recursion Suggested for you C++ recursion C function C user defined function
This is a Python Program to find the power of a number using recursion. The program takes a base and a power and finds the power of the base using recursion. 1. Take the base and exponential value from the user. 2. Pass the numbers as arguments to a recursive function to find the power of the number. 3. Give the base condition that if the exponential power is equal to 1, return the base number. 4. If the exponential power isn’t equal to 1, return the base number multiplied with the power function called recursively with the arguments as the base and power minus 1. 5. Print the final result. 6. Exit. Here is source code of the Python Program to find the power of a number using recursion. The program output is also shown below. def power(base,exp): if(exp==1): return(base) if(exp!=1): return(base*power(base,exp-1)) base=int(input("Enter base: ")) exp=int(input("Enter exponential value: ")) print("Result:",power(base,exp)) 1. User must enter the base and exponential value. 2. The numbers are passed as arguments to a recursive function to find the power of the number. 3. The base condition is given that if the exponential power is equal to 1, the base number is returned. 4. If the exponential power isn’t equal to 1, the base number multiplied with the power function is called recursively with the arguments as the base and power minus 1. 5. The final result is printed.
Subscribe Now: Python Programs Newsletter | Important Subjects Newsletters Case 1: Enter base: 2 Enter exponential value: 5 Result: 32 Case 2: Enter base: 5 Enter exponential value: 3 Result: 125 Sanfoundry Global Education & Learning Series – Python Programs. To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Get Free Certificate of Merit in Python Programming Now! Next Steps:
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.
In this example, you’ll learn to find the factorial of a number using a recursive function. To understand this example, you should have the knowledge of following R programming topics:
The factorial of a number is the product of all the integers from 1 to the number. For example, the factorial of 6 (denoted as 6!) as 1 * 2 * 3 * 4 * 5 * 6 = 720Factorial is not defined for negative numbers and the factorial of zero is one, 0! = 1. In this example, the factorial of a number is calculated using a recursive function. However, you can also calculate it without the recursive function. Learn more about how to find the factorial of a number without recursion. Example: Find Factorial of a number using recursionrecur_factorial <- function(n) { if(n <= 1) { return(1) } else { return(n * recur_factorial(n-1)) } }Output > recur_factorial(5) [1] 120Here, we ask the user for a number and use recursive function recur_factorial() to compute the product upto that number. Lets suppose the user passes 5 to the function. Inside the recur_factorial(), the number 5 is multiplied to the factorial of (5 – 1 = 4). 4 is multiplied again to the factorial of (4 – 1 = 3). This goes on until the number reaches 1. Now, all previous functions of 2, 3, 4 and 5 returns the result one by one giving you the final result 1*2*3*4*5, which equals 120. |