Question

(JAVA-CS113)Does anyone have any recursive practice questions that have solutions, need to study simple ones for...

(JAVA-CS113)Does anyone have any recursive practice questions that have solutions, need to study simple ones for exam like raising a number to a power is a simple one, so ones like this, or any other final exam practice for CS113 java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class PowerOfNumber{
   public static int power(int num, int pow) {
  
if (pow==0) // base condition
return 1;
else
return (num * power(num, pow-1)); // recursive call
}
public static void main(String[] args) {
int num,power;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number : ");
num=sc.nextInt();
System.out.println("Enter a power : ");
power=sc.nextInt();
int result = power(num,power); // function call
System.out.println(num+" to the power "+power+" is "+result);
}
}

OUTPUT:-

// If any doubt please comment

Add a comment
Know the answer?
Add Answer to:
(JAVA-CS113)Does anyone have any recursive practice questions that have solutions, need to study simple ones for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT