Here is the algorithm for javascript: // given integers p, q find their GCD func
ID: 653918 • Letter: H
Question
Here is the algorithm for javascript:
// given integers p, q find their GCD
function GCD(p, q) {
while (q !== 0) {
var temp = q;
// % is the modulus operator. Must get the integer remainder
q = Math.floor(p % q);
p = temp;
}
return p;
}
The project should have the following features:
Input fields for the two numbers
Validation that each number is a positive integer
Correct computation of the two numbers' GCD
Display the calculated GCD and all previous calculated GCDs.
Below is my implementation with a few GCD calculations:
If you have a good memory, you will recognize that this is a lot like the MPG calculator with no popups. In fact that's the project I used as a starting point for this project.
To ensure that you are computing the correct GCD values you can use www.wolframalpha.com. Just enter GCD[number1,number2] and it will print out the GCD value for you. Example:
Explanation / Answer
#include void main() { int num1, num2, gcd, lcm, remainder, numerator, denominator; printf("Enter two numbers "); scanf("%d %d", &num1, &num2); if (num1 > num2) { numerator = num1; denominator = num2; } else { numerator = num2; denominator = num1; } remainder = num1 % num2; while (remainder != 0) { numerator = denominator; denominator = remainder; remainder = numerator % denominator; } gcd = denominator; lcm = num1 * num2 / gcd; printf("GCD of %d and %d = %d ", num1, num2, gcd); printf("LCM of %d and %d = %d ", num1, num2, lcm); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.