# Finding highest common factors of numbers

 Tweet

This C program demonstrates how to calculate HCF highest common factors of 'n' numbers.

You can compile this program on bOtskOOl Free Online C/C++ Compiler

SOURCE CODE

//A C example to find H.C.F. of n Numbers

//***********************
//www.botskool.com
//***********************

#include<stdio.h>
#include<conio.h>
int min(int *temp, int n)
{
int min=temp[0],i;
for(i=0;i<n;i++)
if(temp[i]<min)
min=temp[i];
return min;
}
int is_prime(int num)
{
int i,flg=1;

for(i=2;i<num;i++)
if(num%i==0)
flg=0;

if(flg)
return 1;
else
return 0 ;

}

void main()
{
int flg,flg1,i,j,num[20],temp[20],hcf=1,n;
clrscr();

printf("How many numbers do you wish to enter ? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number %d: ",i+1);
scanf("%d",&num[i]);
//copy to temp variable as well
temp[i]=num[i];
}

//loop to find hcf

for(i=2;i<=min(num,n);i++)
{
if(is_prime(i))
{
flg1=1;
while(flg1)
{
flg=1;
for(j=0;j<n;j++)
{
if(temp[j]%i!=0)
{
flg=0;
flg1=0;
}
}

if(flg)
{
for(j=0;j<n;j++)
temp[j]/=i;
hcf*=i;

}
}
}
}

printf("\n The HCF of given numbers is : %d",hcf);
getch();
}

//***********************
//www.botskool.com
//***********************

Output will look like this:-

Terms of Agreement:
By using this code, you agree to the following terms-
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from us. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which may have placed in the code or code's description.
>>Kindly post your doubts and suggestions on our discussion forum.
 Tweet
Tags: