6 like 0 dislike
285 views

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 (12.1k points)
ID: 27175 - Available when: Unlimited - Due to: Unlimited

retagged by | 285 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
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.174.41
©2016-2023

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)
ID: 25954 - Available when: Unlimited - Due to: Unlimited
| 246 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)
ID: 25955 - Available when: Unlimited - Due to: Unlimited
| 247 views
14,435 questions
231,450 answers
206,247 comments
5,485 users