-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindGreatestCommonDivisorofArray.c
More file actions
49 lines (44 loc) · 1.04 KB
/
FindGreatestCommonDivisorofArray.c
File metadata and controls
49 lines (44 loc) · 1.04 KB
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
#include <stdio.h>
/*Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.*/
int main()
{
int nums[50];
for(int i = 0; i < 50; i++)
{
scanf("Enter elements of array: %d\n", nums[i]);
}
int min, max;
min = nums[0];
max = nums[0];
for(int i = 0; i < numsSize; i++)
{
if(nums[i] > max)
{
max = nums[i];
}
else if(nums[i] < min)
{
min = nums[i];
}
else
{
continue;
}
}
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
int fin = gcd(max, min);
printf("%d", fin);
return 0;
}