Question

JAVA Restrictions 1. don't use any other way to implement a loop but recursion (while and...

JAVA

Restrictions

1. don't use any other way to implement a loop but recursion (while and for are forbidden).

2. don't use Math.pow\

3. Pls do not use while or for


1. write a class called MultiplyByItself with a main method

  2. the program asks for two integer numbers: x and n.  n must be 0
  or greater than 0.

  3. then the program prints x raised to the n-th power.  For example
  if x is 2 and n is 3 the program prints the result of multiplying
  2*2*2 = 8. (see examples)
 
 
 

Examples:

java MultiplyByItself

enter an integer: 2

enter another integer: 0

1

java MultiplyByItself

enter an integer: 2

enter another integer: 1

2

java MultiplyByItself

enter an integer: 2

enter another integer: 3

8

java MultiplyByItself

enter an integer: 2

enter another integer: 10

1024

java MultiplyByItself

enter an integer: 3

enter another integer: 2

9

java MultiplyByItself

enter an integer: 3

enter another integer: 3

27

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

Java Program:

import java.util.*;

class Main {

public static int multiplyByItself(int x,int n){

if(n==0)

return 1;

else

return x*(multiplyByItself(x,n-1));

}

public static void main(String[] args) {

int x,n;

Scanner sc=new Scanner(System.in);

System.out.print("enter an integer:");

x=sc.nextInt();

System.out.print("enter another integer:");

n=sc.nextInt();

System.out.println(multiplyByItself(x,n));

}

}

java version 1.8.0_31 Java (TM) SE Runtime Environment (build 1.8.0 31-b13) Java HotSpot (TM) 64-Bit Server VM (build 25.31

java version 1.8.0_31 Java (TM) SE Runtime Environment (build 1.8.0 31-b13) Java HotSpot (TM) 64-Bit Server VM (build 25.31

if you like the answer please provide a thumbs up.

Add a comment
Know the answer?
Add Answer to:
JAVA Restrictions 1. don't use any other way to implement a loop but recursion (while and...
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
  • java in simple way pla help me 1. Write a program that uses a while loop...

    java in simple way pla help me 1. Write a program that uses a while loop to read 10 integer numbers from file "numbers.in" and prints their sum and average on the screen as follows: The sum is n and average is m. Where n and m are the computed sum and average, respectively.

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

  • 1. Recursion is ususally where a function calls itself; (another example of recursion is where function...

    1. Recursion is ususally where a function calls itself; (another example of recursion is where function A calls function B, and B calls C and C calls A, creating a loop in the calls). Some problems are most naturally solved recursively, such as writing the factorial function, or finding all the perumutations of a sequence, or checking if a string is a palindrome. Since those examples were done in class, here we will give you a toy example, which normally...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • Write a Java program which asks the user to enter an integer x. This program prints...

    Write a Java program which asks the user to enter an integer x. This program prints the remainder of x when it is divided by 3. Example Pl ea s e e n t e r an i n t e g e r : 3 remainde r=0 Pl ea s e e n t e r an i n t e g e r : 17 remainde r=2

  • Design a Java program that asks the user to enter an integer number n and then...

    Design a Java program that asks the user to enter an integer number n and then generates an array of that many random points (x, y) with x- and y-coordinates in the range between 0 and 100. After this the program must ask the user to enter two diagonal points (x_1, y_1) and (x_2, y_2) of a rectangle with sides parallel to the coordinate axis (see the image below, where possible pairs of diagonal points are shown in red color)....

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The...

    JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The interface components will appear centered in the interface, and in two rows. o Row one will have a Label that reads “Please enter a valid integer:” and a Text Field to take in the integer from the user. o Row two will have a Button that when pressed converts the integer to binary  The conversion will be completed using recursion – A separate...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

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
ADVERTISEMENT