Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Compute the Greatest Common Divisor (GCD) using Recursion. Write a recursive method to find the GCD. Write a test program to prompt the user to

Compute the Greatest Common Divisor (GCD) using Recursion. Write a recursive method to find the GCD. Write a test program to prompt the user to enter two integers and displays their GCD.

The findGCD(m, n) method can also be defined recursively as follows: - If m % n is 0, findGCD(m, n) is n; - Otherwise, findGCD(m, n) is findGCD(n, m % n).

Note:

Neither value can be less than 1; verify user input CODE:

import java.util.Scanner;

public class GCDCalculator { public static int findGCD (int m, int n) { // FIXME: Complete the recursive method; If m % n is 0, findGCD(m, n) is n; Otherwise, findGCD(m, n) is findGCD(n, m % n). } public static void main (String[] args) { Scanner scnr = new Scanner(System.in); int num1 = 0; // First input to findGCD int num2 = 0; // Second input to findGCD int gcd = 0; // Result of GCD // FIXME: Complete the statements to prompt the user for inputs System.out.println("Enter the first number: "); System.out.println("Enter the second number: "); // FIXME: Check user values are not less than 1; call recursive findGCD function if (/* Your solution goes in here. */) { System.out.println("Note: Neither value can be less than 1."); } else { gcd = findGCD(num1, num2); System.out.println("The GCD is: " + gcd); } return; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

Discuss all branches of science

Answered: 1 week ago