Program to calculate power of a number using recursion

Description

To find power of number using recursion, power 0 will be the base condition
and for detail follow the below program.

C/C++

/* C program to calculate power of a number using recursion */
//Save it as PowerOfNumberUsingRecursion.c

#include<stdio.h>
int main(){

    int base, power, result;

    printf("Enter base : ");
    scanf("%d",&base);

    printf("Enter power : ");
    scanf("%d",&power);

    result = findPower(base,power);

    printf("%d^%d = %d",base,power,result);

    return 0;
}

int findPower(int base, int power) {

    if(power == 0) {
        return 1;
    }
    return base * findPower(base,(power-1));
}

Output

Input:
Enter base : 
2
Enter power : 
4
Output:
2^4 = 16

Java

/* Java program to calculate power of a number using recursion */
//Save it as PowerOfNumberUsingRecursion.java

import java.io.*;
import java.util.Scanner;
public class PowerOfNumberUsingRecursion {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter base : ");
		int base = scanner.nextInt();
		
		System.out.println("Enter power : ");
		int power = scanner.nextInt();
		
		int result = findPower(base,power);
		
		System.out.println(base+"^"+power+" = "+result);
	}
	private static int findPower(int base, int power) {
		
		if(power == 0) {
			return 1;
		}
		return base * findPower(base,(power-1));
	}
}
Input:
Enter base : 
2
Enter power : 
4
Output:
2^4 = 16

Related Programs

1) Program to calculate power of a number
2) Program to Display Fibonacci Series using Recursion
3) Program to find HCF using Recursion
4) Program to calculate factorial using Recursion
5) Program to find sum of digits of a number
6) Program to reverse a number
7) Program to convert Binary to Decimal
8) Program to convert Octal to Decimal
9) Program to calculate square root of a number without using standard library function sqrt()
10) Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)
Share Me

Leave a Reply