Write a C program to find GCD(HCF) of N numbers using Arrays

By | 06.03.2017

C program to find HCF of N numbers


Greatest Common Divisor

In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that is a divisor of both numbers. For example, the GCD of 8 and 12 is 4.

The greatest common divisor is also known as the greatest common factor (gcf), highest common factor (hcf), greatest common measure (gcm), or highest common divisor.

Related Post : : C program to calculate the GCD(HCF) of number

Example : 

What is the greatest common divisor of 54 and 24?

The number 54 can be expressed as a product of two integers in several different ways:

54\times 1=27\times 2=18\times 3=9\times 6.\,

Thus the divisors of 54 are: 1, 2, 3, 6, 9, 18, 27, 54  

Similarly, the divisors of 24 are: 1, 2, 3, 4, 6, 8, 12, 24

The numbers that these two lists share in common are the common divisors of 54 and 24:

1, 2, 3, 6 .

The greatest of these is 6. That is, the greatest common divisor of 54 and 24. One writes:

                                                                   \gcd(54,24)=6.\,
Here below is the source code of the C program to find HCF of N numbers using Arrays.The program is successfully compile and run(on Codeblocks) on the windows system and produce output below :

SOURCE CODE : :


#include<stdio.h>

int main()
{
    int n,i,gcd;
    printf("Enter how many no.s u want to find gcd : ");
    scanf("%d",&n);
    int arr[n];
    printf("\nEnter your numbers below :- \n ");
    for(i=0;i<n;i++)
    {
        printf("\nEnter your %d number = ",i+1);
        scanf("%d",&arr[i]);
    }
    gcd=arr[0];
    int j=1;
    while(j<n)
    {
       if(arr[j]%gcd==0)
       {
           j++;
       }
       else
       {
           gcd=arr[j]%gcd;
           i++;
       }
    }
    printf("\nGCD of k no.s = %d ",gcd);
    return 0;
}

OUTPUT : :


Enter how many no.s u want to find gcd : 6

Enter your numbers below :-

Enter your 1 number = 100

Enter your 2 number = 75

Enter your 3 number = 35

Enter your 4 number = 260

Enter your 5 number = 330

Enter your 6 number = 1000

GCD of k no.s = 5

Related : : C Program to find GCD of a number using recursion

4.2 10 votes
Article Rating
Category: Arrays Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nithi

else

{

gcd=arr[j]%gcd;

i++;

}

Aniket Saha

why is i++ there???

Yusharth Singh

Struggling with the same

nilesh

I am getting the output as HCF(10,6,16)=4
what is wrong????

nilesh

i think code is wrong;

nilesh

you can see the output for HCF(10,6,16)=4

Screenshot (305).png
Sarvottam

This code only works for sorted array!

ktssatwik

its not i++ there its j++ there just a typing error from their side