Question

In: Computer Science

Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....

Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.

Solutions

Expert Solution

import java.util.*;
public class Main {
        public static int calculatePower(int a,int b)
        {
        if(b==0) return 1;
        else if(b==1) return a;
        else
        return a * calculatePower(a,b-1);
}
        public static void main(String[] args) {
        Scanner scr=new Scanner(System.in);
        int x,y,z;
        System.out.print("\nTo calculate x^y , value of x :");
        x=scr.nextInt();
        System.out.print("\nValue of y :");
        y=scr.nextInt();
        z=calculatePower(x,y);  
        System.out.print("\nx^y :"+z);
        }
}

If you have any questions comment down. Please don't simply downvote and leave. If you are satisfied with answer, please? upvote thanks


Related Solutions

Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x...
Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x and y, as parameters and returns the value xy (x raised to the power y). The exponent must be non-negative. If a negative argument is given for the exponent, then an exception should be thrown. Implement a recursive method called "fib" that takes a positive integer, n, as a parameter and returns the nth Fibonacci value. Assume that the first 2 values in the...
Implement and complement the recursive code to compute the product of two positive integers, x and...
Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product. Hint: consider the following: 6 x 1 = 6 6 x 2 = 6 + (6 x 1) 6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] =...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
A Pythagorean triplet is a set of positive integers (x, y, z) such that x2 +...
A Pythagorean triplet is a set of positive integers (x, y, z) such that x2 + y2 = z2. Write an interactive script that asks the user for three positive integers (x, y, z, in that order). If the three numbers form a Pythagorean triplet the script should a) display the message ‘The three numbers x, y, z, form a Pythagorean triplet’ b) plot the corresponding triangle using red lines connecting the triangle edges. Hint: place the x value on...
What two positive real numbers x and y, with xy=12, minimize 3x+y?
What two positive real numbers x and y, with xy=12, minimize 3x+y?
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an...
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an m-bit number and y is an n-bit number (10 points), and analyze the time complexity of this algorithm (10 points).
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based on the fact that xy = 2(x · (y/2)) when y is even and xy = 2(x · ⌊y/2⌋) + x when y is odd, together with the initial condition xy = 0 when y = 0.
What is the solution to xy''+(1-x)y'+y=0 using the Frobenius method?
What is the solution to xy''+(1-x)y'+y=0 using the Frobenius method?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT