Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in c + + Write a program that computes the greatest common divisor ( GCD ) of two positive integers entered by the user. The

in c++
Write a program that computes the greatest common divisor (GCD) of two positive integers entered by the user.
The GCD of two positive integers (both greater than 0) is the largest integer that divides the two integers. If a and b are positive integers, then d is the GCD of a and b if d is the largest integer that divides a and b. For more information on GCDs, see Greatest common divisor.
Use Euclid's algorithm to compute the GCD. Euclid's algorithm is based on the fact that for any two positive integers a and b, with a > b, the common divisors of a and b are also a common divisor of a - b.
The algorithm for computing the GCD of two positive integers consists of replacing the larger number by the difference of the numbers, and repeating this until the two numbers are equal. The GCD is then the final value of either of the two, now-equal, numbers.
The pseudocode for the algorithm is:
Get the two integers for which to compute the GCD
As long the two integers are not equal:
Set the value of the larger of the two integers to the their difference
Output the GCD, which is the value either of the two integers, since they are now equal
For example, if the user inputs:
4218
the output would be
6
If the user enters a number that is not greater zero, output an error message:
Both numbers must be greater than 0.

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

Principles Of Multimedia Database Systems

Authors: V.S. Subrahmanian

1st Edition

1558604669, 978-1558604667

More Books

Students also viewed these Databases questions

Question

Identify some social performance aspects on which entities report.

Answered: 1 week ago