1 like 1 dislike
7.9k views

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

寫一個輸入兩個整數 輸出它們的最大公因數程式

Hint: Use Euclidean Algorithm

使用歐幾里得演算法

LCM:

In arithmetic and number theory, the least common multiplelowest common multiple, or smallest common multiple of two integers a and b, usually denoted by LCM(ab), is the smallest positive integer that is divisible by both a and b.

What is the LCM of 4 and 6?

Multiples of 4 are:

4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, ...

and the multiples of 6 are:

6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, ...

Common multiples of 4 and 6 are simply the numbers that are in both lists:

12, 24, 36, 48, 60, 72, ....

So, from this list of the first few common multiples of the numbers 4 and 6, their least common multiple is 12.

 

Example input: 

12 28

Example output (first number is GCD, second number is LCM):

4 84

 

[Exam] asked in Midterm by (12.1k points)
ID: 32707 - Available when: 2017-11-15 14:10 - Due to: Unlimited

edited by | 7.9k views

36 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>
int main()
{
* *** * **** **** * a,b,c,d=1,a1,b1;
* * ****** * ** * * **** * ** * * * * * ******* * * * * *
****** * *** ***** ** *
*** ** * **** * * *
* ***** *** **** *** ** *
    {
** **** ***** * * * **** * **** * * * * *** * * *
* ** ** **** ******* * * *** * * * * ** *
*** ** ** * ** * * ** * * ** ** ** * ** * *
    }
* * * ** * * ****** * ** *** * ** **** **** ** * ** **** ** ** %d",b,(a1*b1)/b);
* **** ** * ** * **** ** * * * * *  for(d=a1;d<=a1*b1;d++)
** * * *** * *** * * * ** ***** * * * *
* ** * *** * * * ** * * * ***** * * * * * * ** * **** **** * * * * * * && d%b1==0)
** ** ** ** ***** * * * *** * * * * * * ** ** **** * **** ** * * *** ** * ** * ** *** ** * * * * ** * ****


* ** * * * * * *** ** ** * ***** *



* * ** ** * ** *** * 0;
}
answered by (-329 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<stdio.h>
int main()
{
* * * ** * * ** n,m,i,j,lcm;
* ** * * **** * * *** * * * * ** ** * ** ** ** *
* * ** *** * *** *** * * * ** * * * *
* ** *** ** **
**** * * * **** ** ***** * **** *** * * && m%i==0)
*** * * * ** * * * ** *** * *** * * ** ******

*** * * ** * * ** *
* * *** * ** * ** *** = (n*m)/i;

* ***** *** * * ** * * ***** %d",i,lcm);

** * **** **** * 0;
}
answered
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<stdio.h>
int main(void)
{
** * * *** ** ** ** a,b,i,j,d;
**** *** * * ******* ****** L1,L2;

* *** *** *** *** * *** * * * ****** %d",&a,&b);


** *** *** * *** **** ** **** *
***** ** **** * * **** ****
* ** * * * ** *** ** ** *** ** * ** * * **** && b%i==0 )

* * ** * ** * ** * * * * * * *** ** *****

** * * * * *** * *** * * ** **** *** * ** * * * * * ** * ** * ***** * ** * ** *** ****
** ** ** * * * * ** * * * ** *



    }
****** * * ** ** * * ** ***** ***
    {
** * * * **** *** ** **** *** * *** ****
* * *** ** ** **** *** * * * *** *******
* * *** ** ** * * ** * ** *** *
* ** * * ****** * * * ** * ** ** * ** ***
**** *** ** * * * ***** *** ** * * * *
*** ** * ** * * * * ** * ** ***** ** ** * * ** * * * * ** ** ** * ** ***
** * ***** **** ** *** ** ** * * * *** * *******

    }




* * * * **** ******* ** **** 0;
}
answered by (-249 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include * ******

int main()
{
** ** ** * ** * * * * n = 0;

** * ** * * *** ** m = 0;

* * *** **** * * * * ** multiple_m = 0;

* * ** ** * ** *** * * multiple_n = 0;

* * * * ** ** * *** *** i = 0;

* *** ** ***** ** * ** j = 0;

* * ** * * *** * ** * gcd = 0;

* * * * ** * *** **** ** * * ** *** * %d", &n, &m);

* ** *** *** * * = 1; i < m; i++)
** * ** * ***
* * * ****** *** * ** * *** *** ** ** * * ** * * ** * ** % i == 0 && n % i == 0)
*** * * * * ***** * * ********* * *** ** **
** * ** * * ** ** ******* **** * * *** ** * * * ** ** ** * = i;
*** * ** * ** ** *** * * *** ** *** * *** ****
** * ** * ** * * ** * ***
* * ** *** * ** * ** * * * ", gcd);

** * ***** * ** * ** **** * = 1;i < 100; i++)
*** * * * ** *
**** * * ** *** * * ** * * ** * ** * ** * * ** = m * i;

* * * ** ***** ** * *** ** ** *** * *** * = 1; j < 100; j++)
*** ****** * ******* * ***** ***** * * ** **
* ** *** ******** ******* ** * * *** ********* ** * * ** * ** * ** * = n * j;

** * * ** * ** ** * ** * *** ** * * **** * *** * == multiple_n && multiple_n < 100)
* * ** *** **** ** **** ****** * * *** **** ** * ** ** * * * * * * * ** * * *** * ** * * **** ** * * ** ** multiple_n);
*** * * **** * * * * *** * ** ** ** **
**** * ** * **
}
answered by (-285 points)
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
***** * * *** ** * a,b,i,j,max,min;
* * ** ** * **** * **** * * * ** %d",&a,&b);

*** * *** * ***** * ** * * i>=1; i--)
* * *** *** *** ** * ***
* *** * * *** * ** **** *** * *** * * **** && b%i==0)
* * ****** **** ** *** **** * * *
* ** *** * * * * **** * * *** * ** * **** ** *** * ** *** *** ******** ****
***** ** ** ***** * * ** ** * ** * * ** * * ******** ** **** * *
*** * **** *** ***** * * * ** * ** **

    }

* *** ** ** * ** * * * * j<=a*b; j++)
* * * *** * * ** *
* ** * * ** ** * *** * * *** * * * * * ** *** && j%b==0)
* ** ** *** * ** ** ** * * ***** **** **
*** ** ***** ** * * * * * ** ** **** * ** * * ** * ** *** *** %d",j);
* ** **** * * * ***** * * * ** *** **** * ** *** ** ** ** * ** *
* * ** * * * *** * ** * ** **** ** * ** * * *
* * ** * * **


* ** * ** **** **** 0;
}
answered by (-127 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 <stdio.h>
#include <stdlib.h>

int main()
{
* * * ** ** ** * a,b,i,j,k;
** * * * **** * * * ***** * ** * * ** ** *
** **** ****** * ** ** * * % b != 0)
** * * ** * ***** ** * *
*** ** ***** ** ***** * *** *** ******* * = a % b;
**** * ** * * **** **** ****** *** *** * * ** = b;
*** * ** * * * * ** * * ** ** **** ** * ** = i;
* **** * * * * * * * * *
* * ** * * **** *** *** *** ** *

* ** * * * * * ** * * * 0;
}
35/100 answered by (-255 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
** * * *** ** * * ** a,i,j,k,l;
* * * **** *** * * ** * * * ** *** ****
* ** ** * ** ** ** * = 1 ; i <= a ; i++)
    {
***** *** * ** * *** * *********** * * * * *** * * = i ; j <= a - 1 ; j++)
** * * **** * ** * ** ** * * * **** ** ***** * *** * * ** * ****** ** * ");
** ** * ** * ** *** * * ** ** * * **** ** = 1 ; k <= i ; k++)
* ***** ******* * ** * ** *** ** ** * * * *** ** * * * ** ** * *** ** ***** * **
** *** * **** ** * ** ***** * * ** *** * *** ** * = i - 1 ; l >= 1 ; l--)
*** *** *** ** *** *** * * * * **** * * * *** * ***** *** **** * * * ** ****
*** ***** * * ** * ***** ***** ** ** * ** ** != 0)
* ** ** ** **** ** * * * * ** **** ** **** ****** ***** * * ** *
** *** * * ***** **


* ** ** ** ******* * * * * ***** 0;
}
answered by (-255 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
**** *** *** ** *** * ** a,b,gcd,lcm,c,d;
* * * ***** ** *** ** *** ** * ** * %d",&a,&b);
** * ** **** * * * *
* * ** *** ** * * *
***** ** ** * *** * * **** (a%b!=0)
**** ** * * * *** **
** **** * ** * ** ****** ** *** * * **** ** *****
* *** * * * ** *** * ***** * * ** * * *** ** *
** * * * * ** *** * **** * **
** * ** ** **** ** **
* ** * ** * * ** *** ** * ** *
* * * * * * * * ** * * %d",gcd,lcm);
*** * ** * **** * ** *** ** 0;
}
answered by (-229 points)
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main(void)
{
    int a,b,i,c,n;
    scanf("%d %d",&a,&b);
    if (a>=b)
    {
* ** ** * * ** * * ***** * ** * ** * ***** * (i=a;i>=1;i--)
** ** * * * ** ** * ** * ** * * *
* * * * * * ** ** * **** * *** * * * *** ** * * **** ** * * ** (a%i==0 && b%i==0)
**** * * * * ** * ** * * * ** *** * *** *** *** * * ** * ** ***
**** *** *** ** *** * ** ** * ** ** ** * * * * * * *** * **** ** * * * ** * *** ** * * ",i);
* * * **** * * * * * * *** * * ****** * * * * * * * **** ** * * * * * * * * *
* * **** *** * *** * * ** ** * *** * * ** *** *** *** * * *
* ** *** ** * * * * * * **** **** ** ** *
    }
* * ** * ** ** * ** ** * * * * *** (b>=a)
        {
*** ** * ** * * ** *** ** * *** *** ** *** ****** **** *** * (i=b;i>=1;i--)
        {
**** ** * * * ** * ** ** **** * ** * **** *** *** * ** * * * * (a%i==0 && b%i==0)
** * ** * ** * *** * * ** * * * ** *** ** ** * ** **
** ** * * ** * ********* ** * ************ * * * * * * ** *** **** * **** * * * * ** * ",i);
**** ** * * * ** * * ** * **** * ** ** ***** ** * ** * * * * * * *** * ** ***** * *
*** *** ** ** * * * * * * **** ** * ** ** * * * * * * **** * * *
        }
        }
*** * * * **** * * * * ** * *** ** * *** ** * ** * * * * * * * **
* *** ** * ** ** ***** * * * * * * *** ****** ****** ** * ** * ** **** *** *
* ** *** **** *** * * ** * ** * ** * * **** ** **** ** * ** ** * ** * * ** * * ** * *
* **** * * ****** * * **** * * ** ** * * ******** * ** * * *** * * * *** * * *** * * * * (c%a==0 && c%b==0)
**** * * *** * * **** * **** * ******* *** ***** **** * * * ** ** * ** ** * ****
*** ** * * *** * * * ** ** **** ** * *** **** * ** *** ** * * ** * * *** * * **** * ** ** *** * * * ** ** * *** * * * ** *
* * * ** ** * * * *** *** * * * * * * ** * ** * ** * *** ** **** * ** *** * * ** ** * ** * *** *** * * * * * ** * *** * ** *
* * **** * ****** * * * * *** * ** * ***** * * **** ** * * * * **** * * ****** *
** ** ** * * ** * *** ******** *** * *** ** * *** * **** * **** * ** ** * * ** *
* * * *** ** ******* **** ** * * * * ** ** * * * *** **** * **** * *

** * ** ** * * * ** * ** ** *** * *** **** * 0;
}
answered by (-168 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()
{
* * ** ** ** * ** ** a;
*** ** * *** * * * ** * ** **** * ** *
********* *** * ** * ** * *** ***
* ** * **** ** * 0;



}
answered
0 0
prog.c: In function 'main':
prog.c:5:10: warning: too many arguments for format [-Wformat-extra-args]
    scanf("number",&a);
          ^~~~~~~~
prog.c:6:11: warning: too many arguments for format [-Wformat-extra-args]
    printf("4 84",a);
           ^~~~~~
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.126.17
©2016-2025

Related questions

0 like 0 dislike
10 answers
[Exam] asked Dec 9, 2017 in Midterm by thopd (12.1k points)
ID: 36752 - Available when: 2017-11-15 14:10 - Due to: Unlimited
| 3.4k views
0 like 0 dislike
69 answers
[Exercise] Coding (C) - asked Oct 26, 2017 in Chapter 6: Loops by semicolon (5.2k points)
ID: 28913 - Available when: 2017-10-26 18:00 - Due to: Unlimited
| 14.1k views
0 like 0 dislike
21 answers
[Exam] asked Dec 9, 2017 in Midterm by thopd (12.1k points)
ID: 36754 - Available when: 2017-11-15 14:10 - Due to: Unlimited
| 5.6k views
0 like 0 dislike
22 answers
[Exam] asked Dec 9, 2017 in Midterm by thopd (12.1k points)
ID: 36753 - Available when: 2017-11-15 14:10 - Due to: Unlimited
| 5.6k views
12,783 questions
183,442 answers
172,219 comments
4,824 users