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: 80% (10 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).
-
(A) A handbook lists the aqueous solubility of AgOCN as 7 mg/100 mL at 20 C. What is the K sp of AgOCN at 20 C? (B) A handbook lists the aqueous solubility of lithium phosphate at 18 C as 0.034 g Li...
-
Define a sequence of correlated random numbers \[ s_{k}=\alpha s_{k-1}+(1-\alpha) r_{k} \] where \(r_{k}\) is a unit-variance, uncorrelated, Gaussian pseudorandom number while \(0
-
The Cabin Creek Coal (CCC) Company operates three mines in Kentucky and West Virginia, and it supplies coal to four utility power plants along the East Coast. The cost of shipping coal from each mine...
-
-> Let G and H be groups. A function : G H is called a (group) homomorphism if it satisfies (9192) = (91) * (92) for all 91, 92 G. (Note that the product 91*92 uses the group law in the group G,...
-
I. Do you think treating employees in Tasty Meal is unfair? Justify. Keep the unemployment factor, poverty and economical environment in mind. II. Should Tasty Meal be accused for health issues by...
-
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
-
Consider the set F and the operation of from Problem 34. Does the set F satisfy the given property for the operation of ? Give reasons. a. Associative b. Commutative Data from Problem 34 Consider...
-
Let L be some class of standard latency functions. Let I = (G = (V,A), (la)a=A, (Si, ti)i[k], (ri)i[k]) be a selfish routing instance with latency functions (la)aA L. Define al as the instance that...
-
In this discussion consider what it means to "know."How do you define knowledge?When are we so sure that we cannot be wrong? Consider the following questions When are you so sure that you can't be...
-
Perception of Psychology Question A Psychoanalytic theory suggests that some memories can be so painful, that a person can totally block them from their conscious memory and then later "recover"...
-
Determine the equation of the line tangent to the curve y = 4( y = 4(x + 1) y = at the point (3,16). Determine the equation of the line tangent to the curve y=x3x + 22 at the point (3,21).
-
1 . Explain the differences between 2 sectors economy , 3 sectors economy and 4 sectors economy .?
-
In obtaining representations from lawyers, Jill Malano's aim is to receive the representation letters as early as possible after the balance sheet date. This provides her with a signed letter from...
-
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) {...
-
The city of Austin needs to collect $2,000,000 in property tax revenues. The city estimates 5% of the tax revenues will not be collectible. How much in tax levies should the city of Austin for the...
-
A bakery corporation earns a total revenue of $157,000. Its expenses are $52,000. If the bakery needs to pay 21% corporate income tax, how much tax will they pay? Show your calculations.
-
Year 1: Chip Corp had taxable income before taking a charitable contribution deduction of $25000. They made $4,000 in charitable donations that year. Year 2: Chip Corp had taxable income before...
Study smarter with the SolutionInn App