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

retagged by | 32 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:172.69.69.10
©2016-2019

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.8k points)
Available when: Unlimited - Due to: Unlimited
| 21 views
5 like 0 dislike
3 answers
[Normal] Coding (C) - asked Oct 10, 2017 in Introduction to Computer Programming I (C) by admin (9.8k points)
Available when: Unlimited - Due to: Unlimited
| 17 views
8,033 questions
60,929 answers
33,033 comments
1,100 users