Code For Alls..!

get the code!!

Sunday 10 September 2017

GCD (HCF) of N Integers (Id-3103)

                                                   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.
Output Format:
The first line contains the HCF of these N numbers.
Boundary Conditions:
2 <= N <= 100
Example Input/Output 1:
Input:
4
15 20 30 50
Output:
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]);
}

No comments:

Post a Comment