Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Students also viewed these Databases questions

Question

Describe how and why clinicians classify psychological disorders.

Answered: 1 week ago

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago