Question
Question 4: (25 Marks) Develop a Java program that deals with greatest common dominators GCDs of positive integers. GCD of two positive integers is the
Question 4: (25 Marks) Develop a Java program that deals with greatest common dominators GCDs of positive integers. GCD of two positive integers is the is the largest positive integer that divides each of those two integers. The program works with the following specifications: a. Write a java method getGCD takes as parameter two integers and returns their greatest common divisor. The GCD could be computed for any two positive numbers. If the one of the two integers (or both) is negative the method should return -1. a. In the main method, the program reads from the user the number of pairs of integers to get GCD of each pair. According to the number of pairs, the program prompts the user to enter pairs of integers and saves these numbers in 2 single-dimensional arrays; the first array firstNumbersArray is for the first numbers in the entered pairs, and the second array secondNumbersArray is for the second numbers in the entered pairs. The program should get the GCD of each pair by calling the method getGCD repeatedly and save the produced GCDs in a third single-dimensional array called GCDsArray. The program should count the number of positive pairs that it could compute their GCD. Finally, the program should print all the entered pairs with their GCD and the ratio of the pairs with positive GCDs compared to all entered pairs. ============================================ Sample Run: run: Enter number of pairs of integers: 6 Enter two integers: 44 33 Enter two integers: 24 16 Enter two integers: -8 6 Enter two integers: 50 35 Enter two integers: 9 -5 Enter two integers: 12 8 RESULTS >>>>>>>>> GCD(44,33) = 11 GCD(24,16) = 8 GCD(-8,6) = -1 GCD(50,35) = 5 GCD(9,-5) = -1 GCD(12,8) = 4 Number of positive GCDs = 4 with ratio 66.67% BUILD SUCCESSFUL (total time: 29 seconds) =============================================
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started