Recursive Program to calculate power of a number
Here in this article we will write a Recursive Program to calculate power of a number for this purpose we multiple the number with itself until power become zero. So the base case for this code is if power is zero then we have to return 1.
Recursive Program to calculate power of a number : –
code (C language):-
#include<stdio.h> // function to calculate the power int power(int n,int p) { if(p==0) return 1; else return n*power(n,p-1); } // Driver function int main() { int n,p; printf("Enter a number\n"); scanf("%d",&n); printf("Enter the power to be calculate\n"); scanf("%d",&p); printf("%d to the power %d is %d ",n,p,power(n,p)); return 0; }
C++ code:-
#include<bits/stdc++.h> using namespace std; // recursive function to calculate power int power(int n, int p) { if(p==0) return 1; else return n*power(n,p-1); } int main() { int n,p; cout<<"Enter a number"<<endl; cin>>n; cout<<"Enter the power to be calculate"<<endl; cin>>p; cout<<n<<" to the power "<<p<<" is "<<power(n,p)<<endl; return 0; }
Output (Recursive Program to calculate power of a number ):–
Enter a number 6 Enter the power to be calculate 3 6 to the power 3 is 216
python code:-
def powercal(x,y): if y!=0: return x*powercal(x,y-1) else: return 1 a,b=map(int,input("Enter the number and power ").split()) print(a,"to the power",b,"is",powercal(a,b))
output:-
Enter the number and power 12 2 12 to the power 2 is 144
Recommended Post:-
codechef problems:-
- Primary test
- Sum or difference
- point and line
Wipro :-
- Update the booking ID | Wipro previous year question paper solution
- Pages in PDF
- Find the location id
- Find the odd digits
- Find the Product ID
Infytq :-
Key Points;-
Hackerrank:-
- Python : missing characters : hackerrank solution
- Python : string transformation | Hackerrank solution
- Active Traders certification test problem | Hackerrank Solution
- Usernames changes certification test problem | Hackerrank Solution
- string Representation of objects certification test hackerrank solution
- Average Function | hackerrank certification problem solution
C-tutorial:-
- Micros in C
- Pointer in c
- Function declaration
- Types of user define function
- return type of function
- 2D array
See more:-
- c program to convert specified days into years weeks and days
- Print Reverse Hollow Pyramid
- Update the booking ID | Wipro previous year question paper
- Pages in PDF | Wipro previous year question paper
- Sparse Matrix in data structure
- Find the location ID | Wipro previous year Coding question
- find the odd digits | Wipro Coding question
- Find the product id | Wipro Coding question
- Difference between static and dynamic memory allocation
- What is asymptotic Notation