Question: 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 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, nowequal, 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:
the output would be
If the user enters a number that is not greater zero, output an error message:
Both numbers must be greater than
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
