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
When storage devices write data, instructions, and information on storage media, they are creating output. what best describes t
alukav5142 [94]
Um the data will always be data and mothing will change but it will stay as data just data
5 0
3 years ago
What command launches the remote desktop client for windows?
Ksju [112]
%windir%\system32\mstsc.exe

launches the RDP client.
7 0
3 years ago
write a function that given an integer Y and 3 non-empty string A,B,W, denotingthe year of vacations, the beginning month, the e
chubhunter [2.5K]

Answer:un spain please

Explanation:

4 0
3 years ago
What is the difference between margin and padding property?
VMariaS [17]

Answer:

Margin is applied to the outside of your element hence affecting how far your element is away from other elements.

Padding is applied to the inside of your element hence affecting how far your element's content is away from the border.

Explanation:

Hope it helps!!!

6 0
1 year ago
Which ipv6 header field is known as the priority field?
Yuki888 [10]
<span>Traffic Class header field is known as the priority field.</span>
4 0
3 years ago
Read 2 more answers
Other questions:
  • Based on the Standards, the statement, "Competition for computer time during periods of high demand had become intense because o
    8·1 answer
  • So when i try to click on an assignment to answer it the ask a question thing pops up and i can't exit out of it. I've tried rel
    10·2 answers
  • Can a computer will work more efficiently if you perform disk optimization
    9·1 answer
  • Is there truth? Does truth exists?
    14·2 answers
  • Ex: If the input is: Pat Silly Doe the output is: Doe, P.S.
    9·1 answer
  • Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A
    13·1 answer
  • What makes these Pokémon special?
    9·2 answers
  • . Write at least three benefits of using a network.​
    8·1 answer
  • Unit 2: Lesson 6 - Coding Activity 1
    10·1 answer
  • Paragraph on uses of computer 100 words plss​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!