Create a Java program that implements a recursive function called multiply() which takes two integers inputted by the user and returns the result of their multiplication.
multiply()
main
multiply
4 4
16
import java.util.Scanner; public class RecursiveMultiplyFunction { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number1 = scanner.nextInt(); int number2 = scanner.nextInt(); System.out.println(multiply(number1, number2)); } public static int multiply(int number1, int number2) { if (number2 == 0) { return 0; } else { return number1 + Multiply(number1, number2 - 1); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Create a Java program that implements a recursive function to calculate the result of raising an integer to another integer. This function should be created using recursion.
Create a Java program that implements a recursive function called multiply() that takes two integer numbers requested from the user and returns the result of their multiplication.
Create a Java program that uses recursion to calculate a number in the Fibonacci series.
Create a Java program that implements a recursive function that receives a requested number from the user and returns the factorial of that number.
Create a Java program that requests a string from the user and implements a recursive function to reverse a string of characters.
Create a Java program that implements a recursive function to check if a string is a palindrome or not.
Practice with recursion exercises in Java. Learn to use recursive algorithms and check their efficiency.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.