Here is my code that i have written in gcc. Don't know if my logic is incorrect or i'm making some other mistake.The output is coming as 0 every time.
int main()
{
int n,a[20],count=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int k=0;k<n;k++)
{
int c=0;
for(int j=2;j<n;j++)
{
if(a[k]%j==0)
{
c++;
}
else
{
continue;
}
}
if(c==n)
{
count++;
}
}
cout<<count;
}
You got the loops in the wrong order. You are checking if there are n
dividers of any of the numbers.
Try swapping the loops. Then the count will be of the number of numbers that divide the input.
You also have the wrong upper bound. You need the highest possible divider.
int max_divider = 2;
for (i = 0; i < n; i++) {
if (a[i] > max_divider)
{
// Naive approach
max_divider = a[i];
}
}
// For each number 2..max_divider
for(int j=2;j <= max_divider;j++)
{
int c=0;
for(int k=0;k<n;k++)
{
if(a[k]%j==0)
{
c++;
}
else
{
continue;
}
}
if(c==n)
{
count++;
}
}