GCD IN ARRAY

 Here is Your code :-

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int arr[50], size, max1, max2, gcd, i, j, temp;
    scanf("%d", &size);
    for (i=0; i<size; i++) {
        scanf("%d", &arr[i]);
    }
    for (i=0; i<size; i++) {
        for (j=i; j<size; j++) {
            if (arr[i]>arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    max1 = arr[size - 1];
    max2 = arr[size -2];
    for (i=1; i<=max2; i++) {
        if (max1%i ==0 && max2%i == 0) {
            gcd = i;
        }
    }
    printf("%d", gcd);
    return 0;
}

OUTPUT :-

  

Here 5 is the Input for number of elements in an array and 2 is the GCD of array .

Comments

Popular posts from this blog

WAP to add or to multiply two matrices of order nXn.

WAP that inputs two arrays and saves sum of corresponding elements of these arrays in a third array

WAP to find the minimum and maximum element of the array by using functions.