C program to find GCD of n numbers


Program:

 #include<stdio.h>

int main()
{
    int num, i, j, gcd=1, temp;
    printf("how many numbers are there ? ");
    scanf("%d", &num);
    int array[num];
    printf("Enter %d numbers: \n", num);
    for(i=0; i<num; i++)
        scanf("%d", &array[i]);
   
    j=1;
    i=0;
    while(i != num && j != num)
    {
        temp = 0;
        while(array[i] % array[j] != 0)
        {
            temp = array[i] % array[j];
            array[i] = array[j];
            array[j] = temp;
        }
        i++;
        j++;
    }
    gcd = gcd * temp;
    printf("gcd: %d \n", gcd);
}


Output:


Comments

Popular Posts