1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
harina [27]
3 years ago
10

First write a method to calculate the greatest common divisor (GCD) of two positive integers using Euclid’s algorithm (also know

n as the Euclidean algorithm). Then write a main method that requests two positive integers from the user, validates the input, calls your method to compute the GCD, and outputs the return value of the method (all user input and output should be done in main). Check Wikipedia to find more information about GCDs1 and Euclid’s algorith
Computers and Technology
1 answer:
MAVERICK [17]3 years ago
7 0

Answer:

import java.util.Scanner;

public class Gcd

{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 System.out.print("Enter two numbers to find their GCD: ");

 int number1 = input.nextInt();

 int number2 = input.nextInt();

 

 if(number1 > 0 && number2 > 0)

     System.out.println("GCD of " + number1 +" and " + number2 +" is: " + findGCD(number1, number2));

       else

           System.out.println("Invalid Input!");

}  

public static int findGCD(int number1, int number2) {

    if(number2 == 0){

        return number1;

    }

return findGCD(number2, number1 % number2);

}

}

Explanation:

To be able to find GCD, we need to factorize the numbers and multiply common factors.

- <u>Inside the function:</u>

- Recursively divide <em>number1</em> by <em>number2</em> until <em>number2</em> is equal to zero.

- Return <em>number1</em>  when the <em>number2</em> is equal to zero. That means we found all the divisors of <em>number1</em>

- <u>Inside the main:</u>

- Ask user to input numbers

- Check if both numbers are greater than zero

- If they satisfy the condition, call the method and print the result.

You might be interested in
How to use boolean to check if math answer is correct java
allsm [11]

Boolean can be use to check math as follows;

  • 6 < 5
  • 8 > 7
  • 6 == 9

<h3>What are Booleans?</h3>

Booleans is a datatype with two possible values namely True and False.

The Boolean can be denoted as bool.

In programming its often use to check if a mathematical expression or statement are True or False.

Therefore, let's use it in mathematical expression;

6 < 5

8 > 7

6 == 9

The first expression will return False.

The second expression will return True.

The third expression will return False.  

learn more on Booleans here: brainly.com/question/14120893

#SPJ11

7 0
2 years ago
Read 2 more answers
A server-side extension ________________. provides its services to the web server in a way that is totally transparent to the cl
damaskus [11]
A: provides its services to the Web server in a way that is totally transparent to the client browser.
8 0
3 years ago
Supervisor: You will need to consolidate your trouble tickets
liubo4ka [24]
Wheres the rest???????????????????
7 0
2 years ago
Explain four features of presentation packages​
hichkok12 [17]
You have add more for anyone to be able to answer this, sorry
4 0
2 years ago
Select the best answer from the drop-down menu. ________ networks use cables connected directly to the computer. Signals sent in
LekaFEV [45]

Answer: Wired, Wi-fi, Wi-fi, A wired network, Wi-fi

Explanation:

4 0
3 years ago
Other questions:
  • Question 14. (3.04 MC) how does the project manager evaluate the scope of a project
    14·1 answer
  • When did the digital revolution begin
    10·1 answer
  • How was windows 3 installed on a pc?
    11·1 answer
  • A network address is 131.247.160.0/19. The 19 implies that
    5·1 answer
  • What is 450 g of flour a measure of?
    11·1 answer
  • Courses that enable students to begin jobs that require course completion certificates are know as
    8·1 answer
  • PLEASE HELP ME!!!
    13·1 answer
  • Convert alzebric expression into basic expression<br> (1/2)bh<br> PLEASE GIVE ANSWER FAST
    10·1 answer
  • Which version of HTML provides the lowest common denominator of HTML tags
    8·1 answer
  • the human resources department requests a list that contains the number (i.e. count) of orders taken on each date, grouped by th
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!