Question
This assignment is to write a program that asks the user for a positive integer (greater than zero). Verify that you have received a valid
This assignment is to write a program that asks the user for a positive integer (greater than zero). Verify that you have received a valid number and trap the user if they have not given a valid input value. Ask the user for another positive integer and verify this one also. Notice that the operation of getting a positive integer is the same in both cases. Use a single function, e.g. getInt(), for this task that returns the positive integer that the user types in. Then you can simply call the function twice to get the values that you need.
Once you have two positive integers, use a function, which you must call getGCD. Your function will take 2 positive integers as arguments and return the largest integer that evenly divides both of the input values (the GCD). The function should compute this value using Euclid's method; gcd(a, 0) = a and if b > a then gcd(a, b) = gcd(b mod a, a). More information on the strategy is available on the linked article. Your function will return the GCD to the main function. Print the result from the main function (not within the getGCD function). Notice that you will need to write at least 2 functions, one to get a valid user input and one to calculate. Your program output should look like the following:
Please enter a positive integer: -1 I'm sorry, that number is unrecognized or not positive. Please enter a positive integer: 0 I'm sorry, that number is unrecognized or not positive. Please enter a positive integer: stop I'm sorry, that number is unrecognized or not positive. Please enter a positive integer: 20 Please enter a second positive integer: 8 The largest integer that divides both 20 and 8 is: 4Step 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