{"id":9598,"date":"2021-09-30T17:30:29","date_gmt":"2021-09-30T12:00:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=9598"},"modified":"2021-11-22T18:33:32","modified_gmt":"2021-11-22T13:03:32","slug":"python-program-to-find-the-power-of-a-number-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-power-of-a-number-using-recursion\/","title":{"rendered":"Python Program to Find the Power of a Number Using Recursion"},"content":{"rendered":"
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list<\/a> available.<\/p>\n Power of a number :<\/strong><\/p>\n A number’s power (or exponent) aa represents the number of times xx must be multiplied by itself. It is written as a tiny number above and to the right of the base number.<\/p>\n Recursion:<\/strong><\/p>\n If you’re familiar with Python functions, you’ll know that it’s typical for one function to call another. It is also feasible for a function in Python to call itself! A recursive function calls itself, and the process of using a recursive function is known as recursion.<\/p>\n Although it may appear strange for a function to call itself, many sorts of programming challenges are better stated recursively.<\/p>\n Given a number N and the power of P. The aim is to develop a Python program that uses recursion to find the power of a number with the given base.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the power of a number using the recursive approach in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available. Power of a number : A number’s power (or exponent) aa represents the number of times xx must be multiplied by itself. It …<\/p>\nEnter some random base =8 \r\nEnter some random exponent value = 3<\/pre>\n
8 ^ 3 = 512<\/pre>\n
Enter some random base =17\r\nEnter some random exponent value = 3<\/pre>\n
17 ^ 3 = 4913<\/pre>\n
Program to Find the Power of a Number Using Recursion in Python<\/h2>\n
\n
1)Using Recursion(Static Input)<\/h3>\n
\n
# function which calculates the power of the number recursively\r\ndef powerRecursion(given_base, given_exp):\r\n # Give the base condition in the instance where the exponent argument is 1.\r\n if(given_exp == 1):\r\n return(given_base)\r\n # If the exponent is not equal to 1, return the base multiplied by the function\r\n # with the parameter's base and exponent minus 1.\r\n if(given_exp != 1):\r\n # Until the exponent value is 1, the function calls itself.\r\n return(given_base*powerRecursion(given_base, given_exp-1))\r\n\r\n\r\n# Give the base as static input and store it in variable.\r\ngiven_base = 4\r\n# Enter some random exponent as static input and store it in a variable\r\ngiven_exp = 11\r\n# passing the given base an exponent as arguments to the recursive function powerRecursion\r\nprint(given_base, \"^\", given_exp, ' = ', powerRecursion(given_base, given_exp))\r\n<\/pre>\n
4 ^ 11 = 4194304<\/pre>\n
2)Using Recursion(User Input)<\/h3>\n
\n
# function which calculates the power of the number recursively\r\ndef powerRecursion(given_base, given_exp):\r\n # Give the base condition in the instance where the exponent argument is 1.\r\n if(given_exp == 1):\r\n return(given_base)\r\n # If the exponent is not equal to 1, return the base multiplied by the function\r\n # with the parameter's base and exponent minus 1.\r\n if(given_exp != 1):\r\n # Until the exponent value is 1, the function calls itself.\r\n return(given_base*powerRecursion(given_base, given_exp-1))\r\n\r\n\r\n# Give the base as user input using int(input()) function and store it in a variable.\r\ngiven_base = int(input(\"Enter some random base =\"))\r\n# Give some exponent as user\u00a0input\u00a0using\u00a0int(input())\u00a0function and store it in a variable\r\ngiven_exp = int(input(\"Enter some random exponent value = \"))\r\n# passing the given base an exponent as arguments to the recursive function powerRecursion\r\nprint(given_base, \"^\", given_exp,' = ',powerRecursion(given_base, given_exp))\r\n<\/pre>\n
Enter some random base =8\r\nEnter some random exponent value = 3\r\n8 ^ 3 = 512<\/pre>\n
\n
\n