Question: Assignment: Euclids algorithm for finding the greatest common divisor (gdc) of two numbers is fairly simple, but there are some possible problems that we will

Assignment: Euclids algorithm for finding the greatest common divisor (gdc) of two numbers is fairly simple, but there are some possible problems that we will guard against.

The algorithm: given two numbers, n1 and n2:

  1. Divide n1 by n2 and let r be the remainder.
  2. If the remainder r is 0, the algorithm is finished and the answer is n2. (If the remainder is 1, the numbers are mutually prime-see below.)
  3. Set n1 to the value of n2, set n2 to the value of remainder r and go back to step 1.

There are some things to watch out for:

  • For the purposes of this exercise use integer values for n1 and n2.
  • Entering 0 for one of the values is bad. It should work for the other value, but you have to figure out which is OK and which is bad.
    • Catch this problem as it happens and make the user enter another value until they enter an acceptable one.
    • Give an appropriate error message if this happens.
  • The user should be prompted to enter values for n1 and n2.
    • For our purposes entering either n1 or n2 less than zero is an error:
      • Catch these errors and make the user enter another value until they enter an acceptable one for the value in error.
      • Give an appropriate error message if this happens.
      • Hint: the program code for catching errors in the entry of n1 and n2 is different.
  • If the remainder is 1, the two numbers are mutually prime:
    • This means that they have no common divisors except 1.
    • In this case print a message informing the user of that circumstance
  • If the two numbers have a greatest common divisor other than 1, print the two numbers and their gcd in an informative message.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!