Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The greatest common divisor (GCD) of two nonnegative integers a, b is the greatest integer that both a and b are divisible byor zero if

The greatest common divisor (GCD) of two nonnegative integers a, b is the greatest integer that both a and b are divisible byor zero if a = b = 0. We can find the GCD of a and b using the following algorithm, known as the Euclidean Algorithm. Algorithm EUCLID Input: nonnegative integers a, b. Output: gcd(a,b). Procedure: Repeat

If a=0, then return b as the result.

Otherwise, replace a, b with b mod a, a. Above, b mod a is the remainder when b is divided by a.

In C++, we compute this using b % a. For example, suppose a=24 and b=57. We apply the above algorithm. The value of a is not zero, so we are not done. Dividing 57 by 24, we get the remainder 9. So our new values are a=9 and b=24. Continuing in this manner, we obtain the following values of a=0 and b=3. a b 24 57 9 24 6 9 3 6 0 3 Since we now have a=0, the algorithm returns the value of b: 3. Instructions Write a complete C++ program that repeatedly inputs two nonnegative integers from the user, with proper error checking. The program prints the GCD of the two integers, and then repeats. There should be some way the user can end the program. The source code for the function should be in file gcd.cpp. The program should compute the GCD by calling a function that implements Algorithm EUCLID (described above). Proper error handling should be done. In particular, if the user is asked to enter a number, and something else is typed, then the program should notify the user and retry the input.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

Students also viewed these Databases questions