Answered step by step
Verified Expert Solution
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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started