Find the power of a number using recursion

Problem statement:- Program to Find the power of a number using recursion.

Data requirement:-

   Input Data:- num1, num2

  Output Data:-sum(num1,num2)

Program in C

Here is the source code of the C Program to Find the power of a number using recursion.

Code:

#include<stdio.h>
int Power(int num1, int num2)
{
   if(num2==0)
    return 1;
   return(num1*Power(num1, num2-1));
}
int main()
{
    int num1, num2;
    printf("Enter the base value:");
    scanf("%d",&num1);
    printf("Enter the power value:");
    scanf("%d",&num2);
    printf("Power of Number Using Recursion is:%d",Power(num1,num2));
}

Input/Output:
Enter the base value:5
Enter the power value:2
Power of Number Using Recursion is:25

Program in C++

Here is the source code of the C++ Program to Find the power of a number using recursion.

Code:

#include<iostream>
using namespace std;
int Power(int num1, int num2)
{
   if(num2==0)
    return 1;
   return(num1*Power(num1, num2-1));
}
int main()
{
    int num1, num2;
    cout<<"Enter the base value:";
    cin>>num1;
    cout<<"Enter the power value:";
    cin>>num2;
   cout<<"Power of Number Using Recursion is:"<<Power(num1,num2);
}

Input/Output:
Enter the base value:10
Enter the power value:2
Power of Number Using Recursion is:100

Program in Java

Here is the source code of the Java Program to Find the power of a number using recursion.

Code:

import java.util.Scanner;
public class PowerOfTwoNumberUsingRecursion {
static int Power(int num1, int num2)
{
   if(num2==0)
    return 1;
   return(num1*Power(num1, num2-1));
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1, num2;
System.out.print("Enter the base value:");
    num1=cs.nextInt();
    System.out.print("Enter the power value:");
    num2=cs.nextInt();
    System.out.print("Power of Number Using Recursion is:"+Power(num1,num2));
        cs.close();
}

}

Input/Output:
Enter the base value:35
Enter the power value:5
Power of Number Using Recursion is:52521875

Program in Python

Here is the source code of the Python Program to Find the power of a number using recursion.

Code:

def Power(num1,num2):
    if num2==0:
        return 1
    return num1*Power(num1, num2-1)
num1=int(input("Enter the base value:"))
num2=int(input("Enter the power value:"))
print("Power of Number Using Recursion is:",Power(num1,num2))

Input/Output:

Post a Comment

0 Comments