Showing posts with label Counting common factors. Show all posts
Showing posts with label Counting common factors. Show all posts

Monday 25 February 2019

Count of common factors

Given a set of numbers where all other numbers are multiple of the smallest number, the program must find the count of the common factors C excluding 1.

Input Format:
First line will contain the integer value N representing how many numbers are passed as input.
Next N lines will have the numbers.

Output Format:
First line will contain the count of common factors C.

Constraints:
N will be from 2 to 20.

Sample Input/Output:
Example 1:
Input:
2
100
75

Output:
2
Explanation:
The common factors excluding 1 are 5,25. Hence output is 2




Program
#include<stdio.h>
#include <stdlib.h>

int main()
{
   int N,A[100],i,cnt=0,j,small;
   scanf("%d",&N);
   for(i=0;i<N;i++)
      scanf("%d",&A[i]);
    small=A[0];
    for(i=0;i<N;i++)
      if (A[i]<small)
        small=A[i];
   j=2;
   while (j<=small)
   {
     for(i=0;i<N;i++)
      if (A[i]%j!=0)
         break;
      if (i==N)
        cnt++;
     j++;
   }
   printf("%d",cnt);

}

OUTPUT
Input:
3
10
20
30
Output:
3