GCD of n numbers

Program:

#include <stdio.h>
int gcd(int a,int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int findgcd(int a[],int n)
{
    int result=a[0];
    for(int i=0;i<n;i++)
    {
        result=gcd(a[i],result);
    }
    return result;
}
int main() {
    int test;
    scanf("%d",&test);
    for(int i=0;i<test;i++)
    {
        int size;
        scanf("%d",&size);
        int a[size];
        for(int j=0;j<size;j++)
        {
            scanf("%d",&a[j]);
        }
        printf("%d \n",findgcd(a,size));
    }
    return 0;
}

Input:

5
5 2 4 8 16 32
4 3 6 9 12
3 5 10 15
2 50 100
1 30
2
3
5
50
30

Comments

Popular posts from this blog

C program to print the following Pattern

c++ program to print string along the diagonals of the matrix.

Kollywood Game