Program to delete duplicates from a link list

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:-

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-

C-tutorial:-

See more:-

Leave a Reply

Your email address will not be published.