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
In the ____________________ technique, the virus is divided into several parts and the parts are placed at random positions thro
shusha [124]

Answer:

Split Infection

Explanation:

In the split infection technique, each time the file is opened either by the user or the system operating system, the virus copy itself into another file on the same system and unload malicious payloads to perform some actions. While this is ongoing, the virus erase file from the user hard drive and might even go as formatting the hard disk drive.

It should be noted that the virus cannot transfer itself from one computer to another

5 0
3 years ago
It would be at least two decades before some of
Ivan

Answer:

yes! of course and sure! okay

6 0
2 years ago
Hellpppppppppppppppp
jonny [76]

Answer:

i can't see your problem

6 0
2 years ago
Read 2 more answers
What is the ethical danger of using agents in negotiation?
Mariulka [41]
Information sharing, divergent interests, conflicting interests
6 0
3 years ago
How do I give Brainliest? Will give brainliest when I figure out how
Lina20 [59]
When somebody writes the answer for your question and posts it on your question it will show a “(crown)mark as brainliest” in blue. click it and it’ll turn yellow and mark the person the brainliest for your question
7 0
3 years ago
Read 2 more answers
Other questions:
  • What is the resistance of a 1,000-foot length of #6 AWG wire at a temperature of 25 degrees C? A. 0.1593 ohm B. 0.4028 ohm C. 0.
    7·1 answer
  • Assume the following variable definitions int a = 5, b = 12; double x = 3.4, z = 9.1. What are the values of the following expre
    7·1 answer
  • What component of Kerberos is responsible for storing keys for encrypting and decrypting data in the authentication process?
    13·1 answer
  • Name a piece of software you often use where it is easy to produce an error. Explain ways you could improve the interface to bet
    5·1 answer
  • Is a pocket watch consider a computer
    15·1 answer
  • How is science and technology used in the society​
    11·1 answer
  • The Arizona Department of Transportation has offices throughout the state. State documents are stored on a large server in a cen
    5·2 answers
  • In what way, if any, is the impact of a given risk affected by the timing of a project?
    8·1 answer
  • Viruses that load from usb drives left connected to computers when computers are turned on are known as.
    9·1 answer
  • Which of the following declares an abstract method in an abstract Java class?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!