6 like 0 dislike

Write a program that asks the user to enter two integers, then calculates and displays their greatest common divisor (GCD).

Hint: The classic algorithm for computing the GCD, known as Euclid's algorithm, goes as follow: let m and n be variables containing the two numbers. If n is 0, then stop: m contains the GCD. Otherwise, compute the remainder when m is divided by n. Copy n into m and copy the remainder into n. Then repeat the process, starting with testing whether n is 0.

Example input: 

12 28

Example output:

4

 

[Normal] Coding (C) - asked in Introduction to Computer Programming I (C) by (12k points)
Available when: Unlimited - Due to: Unlimited

retagged by | 93 views

2 Answers

0 like 0 dislike
Hidden content!
#include * * * *** *
int main()
{
* * *** * *** ** * n1, n2, i, gcd;
** **** * * * ** * ** * * * %d", &n1, * ****
* *** * *** ** ** * *
** * ******** ** * ** ** * ** i <= n1 *** * ** i <= n2; ++i)
* * * * * * *** *
** * * *** *** *** * *** ** **** ** *** * ** * * Checks if i is factor of both integers
** * * ** **** ** * **** *** * *** ** * * ** **** n2%i==0)
* *** * *** *** * *** * * * * * * * ** **** **** = i;
* ** ** * ** * ** *
* * * * * * * ***** *
* *** * ** **** * * ** * * ** * * *** * ** gcd);
** * *** * * * ** 0;
}
answered by (-233 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include ** ** ** *
int main()
{
** ** * ** * ** * ** * n1, n2, i, gcd = 0;
* * ** * * * * *
* * * ***** **** *** * ** * * ** *** ** * * * * %d", &n1, &n2);
* ** * * ** * ** * *
* * ** * ***** * * ** i <= n1 && i <= n2; ++i)
** ***** * *** **
** * ** *** *** * * * ** **** ** * ***** * ** * ** && n2%i==0)
* ** ** *** ** * * * * * * * * ** * * * ** *** ** * * * = i;
* *** ** **** *** *
** ** * * ** **
***** * * ******* **** * * * * ** * ** *
* ** * ** * * * *
* *** * * * ** * ** 0;
}
answered by (323 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Get it on Google Play Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.158.79.253
©2016-2021

Related questions

6 like 1 dislike
1 answer
[Normal] Coding (C) - asked Oct 10, 2017 in Introduction to Computer Programming I (C) by admin (9.9k points)
Available when: Unlimited - Due to: Unlimited
| 105 views
5 like 0 dislike
3 answers
[Normal] Coding (C) - asked Oct 10, 2017 in Introduction to Computer Programming I (C) by admin (9.9k points)
Available when: Unlimited - Due to: Unlimited
| 66 views
11,166 questions
132,626 answers
131,350 comments
3,823 users