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
What two internetworking devices does xyz schools have to install?
patriot [66]
PC's and LAN spreaders* or internet network extenders.
*if the school uses a LAN network.
7 0
4 years ago
Complete two examples of how scientists, technologists, engineers, and mathematicians may work together to create a new product
asambeis [7]

Answer:

By thinking of ways to create environment friendly products such as biodegradable water bottles

3 0
3 years ago
Suppose an AI scientist designed a robot to ring an alarm for food. If the clock strikes 1, the robot displays a message “Time f
Rudik [331]

Suppose an AI scientist designed a robot to ring an alarm for food. If the clock strikes 1, the robot displays a message “Time for lunch.” It displays such messages at breakfast and dinnertime, too. What kind of agent is the robot?

A. model-based reflex agent

B. goal-based agent

C. utility-based agent

D. simple reflex agent

4 0
2 years ago
Read 2 more answers
Im doing a computer project and I would need some help
lisabon 2012 [21]
- Tons of celebrities live in California. You have the kardashians, youtubers and internet stars, musicians, millionaires, etc.

-California is known for gold, silicon, etc.

-Some words that describe California are "economic", "melting pot", "adventurous" "beaches" "Hollywood" "sunny" etc.
6 0
3 years ago
Write a python program to calculate the sum of numbers from 1 to 20 which is divisible by 3.
Ksivusya [100]

Answer:

Input : N = 5

Output : 7

sum = 3 + 4

Input : N = 12

Output : 42

sum = 3 + 4 + 6 + 8 + 9 + 12

4 0
3 years ago
Read 2 more answers
Other questions:
  • A cracked tone (reluctor) ring will often cause what type of problem
    13·1 answer
  • A wireless mesh network with 17 computers would need _______________ connections.
    13·1 answer
  • What's is the contribution of technology to the country?
    15·1 answer
  • What is TLB for? Why TLB? Given the following number, what is theeffective memory access time?
    11·1 answer
  • A skillful response consist of two parts: affirmation andanswer<br><br> True<br><br> False
    10·1 answer
  • please help me!!!!!! all of the following may be benefits of a technical or vocational college except
    13·2 answers
  • 4.2.10: Multiplication Table: Create a program using a for loop which will print out the 4’s times table through 10.
    9·1 answer
  • PLZ HELP ME!!
    8·2 answers
  • write two paragraphs (at least 5 sentences each) about what President Biden and Vice-President Harris plan to do during the firs
    6·1 answer
  • Brianna is taking a backpacking trip in the wilderness and wants to back up the photos from her camera. Which type of storage de
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!