GCD (HCF) of N Integers (Id-3103)
N integers are passed as input to the program. The program must print the GCD (HCF) of these N integers.
Input Format:
The first line contains N.
The second line contains N integers each separated by a space.
The second line contains N integers each separated by a space.
Output Format:
The first line contains the HCF of these N numbers.
The first line contains the HCF of these N numbers.
Boundary Conditions:
2 <= N <= 100
2 <= N <= 100
Example Input/Output 1:
Input:
4
15 20 30 50
Input:
4
15 20 30 50
Output:
5
5
c program:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n,t;
int a[n];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n-1;i++)
{
while(a[i+1]!=0)
{
t=a[i+1];
a[i+1]=a[i]%a[i+1];
a[i]=t;
}
a[i+1]=a[i];
}
printf("%d",a[n-1]);
}
#include <stdlib.h>
int main()
{
int n,t;
int a[n];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n-1;i++)
{
while(a[i+1]!=0)
{
t=a[i+1];
a[i+1]=a[i]%a[i+1];
a[i]=t;
}
a[i+1]=a[i];
}
printf("%d",a[n-1]);
}
No comments:
Post a Comment