Learn-dsa..in 30 days!



























CC-14 : Implement power function.

Description:

Given an input number and input integer named pow, return the value of num to the power pow.

Test cases and expected outputs:

Input Parameters Expected outputs
Number: 8
Power: 2
NumToPow: 64
Number: 2
Power: 5
NumToPow: 32

Pseudocode:

numToPow(num, pow):

If pow==0, return 1; (base case).
Else:
Return product of num and return value of recursive call to numToPow(num,pow-1).

Code:

public int numToPower(int num, int pow){
	if (pow == 0) {
		return 1;
	}else {
		return num*numToPower(num, pow-1);
	}
}

Click here to download and run code and test cases !