Another solution for Listing 5.9 to find the greatest common divisor of two integers n1 and n2
Question:
Another solution for Listing 5.9 to find the greatest common divisor of two integers n1 and n2 is as follows: First find d to be the minimum of n1 and n2, then check whether d, d-1, d-2, . . . , 2, or 1 is a divisor for both n1 and n2 in this order. The first such common divisor is the greatest common divisor for n1 and n2. Write a program that prompts the user to enter two positive integers and displays the gcd.
Listing 5.9
Transcribed Image Text:
1 import java.util.Scanner; 2 3 public class GreatestCommonDivisor { /** Main method */ public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner (System.in); 5 // Prompt the user to enter two integers System.out.print("Enter first integer: "); int n1 = input.nextInt(); System.out.print("Enter second integer: "); int n2 = input.nextInt(); 10 11 12 13 14 15 int gcd = 1; // Initial gcd is 1 int k = 2; // Possible gcd while (k <= n1 && k <= n2) { if (nl % k == 0 && n2 % k == 0) gcd = k; // Update gcd k++; 16 17 18 19 20 21 22 + n1 + 23 24 25 System.out.println("The greatest common divisor for and " + n2 + " is " + gcd); 26 } Enter first integer: 125 JEnter Enter second integer: 2525 JEnter The greatest common divisor for 125 and 2525 is 25
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 75% (12 reviews)
Output Enter first integer 125 Enter second integer 2525 The greatest common divisor for 12...View the full answer
Answered By
Ali Khawaja
my expertise are as follows: financial accounting : - journal entries - financial statements including balance sheet, profit & loss account, cash flow statement & statement of changes in equity -consolidated statement of financial position. -ratio analysis -depreciation methods -accounting concepts -understanding and application of all international financial reporting standards (ifrs) -international accounting standards (ias) -etc business analysis : -business strategy -strategic choices -business processes -e-business -e-marketing -project management -finance -hrm financial management : -project appraisal -capital budgeting -net present value (npv) -internal rate of return (irr) -net present value(npv) -payback period -strategic position -strategic choices -information technology -project management -finance -human resource management auditing: -internal audit -external audit -substantive procedures -analytic procedures -designing and assessment of internal controls -developing the flow charts & data flow diagrams -audit reports -engagement letter -materiality economics: -micro -macro -game theory -econometric -mathematical application in economics -empirical macroeconomics -international trade -international political economy -monetary theory and policy -public economics ,business law, and all regarding commerce
4.00+
1+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Section B.1 includes a C program that calculates the greatest common divisor of two integers. a. Describe the algorithm in words and show how the program does implement the Euclid algorithm approach...
-
If x[n] is periodic of period N 1 > 0 and y[n] is periodic of period N 2 > 0 (a) What should be the condition for the sum z[n] of x[n] and y[n] to be periodic? (b) What would be the period of the...
-
Modify Listing 5.15 to display all the prime numbers between 2 and 1,000, inclusive. Display eight prime numbers per line. Numbers are separated by exactly one space. Listing 5.15 1 public class...
-
As the company accountant, the management of Nickle Company asks you to explain (a) the concept of reasonable assurance in internal control and (b) the importance of the human factor in internal...
-
Represent pictorially that x log x iso (x2) by graphing x log x, / x2, and x log x/x2. Explain how this picture shows that x log x is o(x2).
-
Derive cost of goods sold from data in the statement of cash flows. The section showing cash flow from operations, using the indirect method, for General Electric Companj reported an increase in...
-
A company paid $326,000 for property that included land, land improvements, and a building. The land was appraised at $175,000, the land improvements were appraised at $70,000, and the building was...
-
Frederick (Freddy) Free weights (age 42) owns and operates a health club called Treadmills West. The business is located at 4321 Arnold Street, Alpine, CA 91901. Freddy had the following income and...
-
Problem #5 On January 2, 2018 Bobby Wino, Inc. purchased a mondo grande laserized pizza oven for which the following information is available: Original cost.... Estimated residual value...... Useful...
-
A pyridine-water solution, 50 wt% pyridine, is to be continuously and countercurrently extracted at the rate of 2.25 kg/s with pure chlorobenzene to reduce the pyridine concentration to 2 wt%. (a)...
-
Use a while loop to find the largest integer n such that n3 is less than 12,000.
-
Write a nested for loop that prints the following output: 2 4 2 8. 1. 4 4 2 4 8. 8 16 16 16 8 16 32 32 4 4 . 8 16 32 64 4 2 16 32 64 8. 2 4 16 32 64 128 16 8 1. 4
-
Triangle ABC is such that AB = 4 cm, BC = 6 cm and ACB = 36. Show that one of the possible values of ABC is 25.8 (to 3 s.f.). Using this value, calculate the length of AC.
-
Analyze the beginning and ending cash positions from your statement of cash flows from quarter six (Q6) for each of the following: cash flow from operating activities cash flow from investing...
-
It is your job to make sure that all codes are assigned by 5:00 PM each day so that bills get dropped within three days of a patient's discharge. You know you will be reprimanded if this does not...
-
The operating system is the physical environment where applications run. Any vulnerability in the operating system could compromise the security of one or more applications. Threats can come through...
-
For Project , create an infographic that represents YOUR personal brand. Use the work we've done in class (mission statements, skill inventory, etc.) and develop an infographic about YOU that will...
-
Catena's Marketing Company has the following adjusted trial balance at the end of the current year. Cash dividends of $685 were declared at the end of the year, and 500 additional shares of common...
-
Webster Street Partnership owned real estate in Omaha, Nebraska. It leased an apartment to Matthew Sheridan and Pat Wilwerding for one year at a rental of $250 per month. Although Webster Street did...
-
Comptech Ltd is a manufacturer of optical equipment. In September 2019, Ed Thompson the Chief Research Officer, attended a conference in Switzerland that focused on optical developments for the 21st...
-
Write a test class to test the methods length, charAt, substring, and indexOf in the java.lang.String class.
-
Write a test class to test the methods add, remove, addAll, removeAll, size, isEmpty, and contains in the java.util.HashSet class.
-
Write a test class to test the method isPrime in Listing 6.7 PrimeNumberMethod. java. Data from Listing 6.7 1 public class PrimeNumber Method { 2 pub11c static void main(String[] args) {...
-
Rotan, Inc. purchased a van on January 1, 2018, for $800,000. Estimated life of the van was five years, and its estimated residual value was $96,000. Rotan uses the straightline method of...
-
If at the beginning of a period, you buy a share of stock for $49, then receive a dividend of $3, and finally sell the stock for $51, what was your holding period return? 9.3% 10.2% 14.8% 16.3%
-
The cumulative effect of most changes in accounting principle is reported: Multiple Choice In the balance sheet accounts affected. In the income statement before income from continuing operations. In...
Study smarter with the SolutionInn App