-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd lcm version 2.c
More file actions
57 lines (51 loc) · 941 Bytes
/
Copy pathgcd lcm version 2.c
File metadata and controls
57 lines (51 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<stdio.h>
#include<stdlib.h>
int filter(const void * b,const void* a)
{
return (*(int *)b - *(int *)a);
}
int gcm(int a,int b)
{
int i;
while(b!=0)
{
int temp;
temp=b;
b=a%b;
a=temp;
}
return a;
}
int lcm(int a,int b)
{
return (a*b)/gcm(a,b);
}
int main()
{
int count=0;
int capacity=1;
int *arr=(int*)malloc(capacity*sizeof(int));
int input,i,b;
printf("ENTER:");
while(scanf("%d",&input)==1 )
{
if(count==capacity)
{
arr=(int*)realloc(arr,capacity*2*sizeof(int));
}
arr[count++]=input;
}
qsort(arr, count, sizeof(int), filter);
int a=arr[0];
int l=arr[0];
for (i=1;i<count;i++)
{
int b=arr[i];
a=gcm(a,b);
l=lcm(l,b);
}
printf("GCD VALUE: %d\n",a);
//LCM
printf("lcm VALUE: %d",l);
free(arr);
}