You are on page 1of 1

12.

Write a program that computers the gcd and the lcm of all the elements found inside a given array,
Use the subprogram defined at problem nr. 11

Sample:

INPUT OUTPUT
4
6 72
12 24 18 36

Programul C++ (in MinGW)

#include<iostream>
uses namespace std;
int cmmdc(int x , int y)
{
while(x!=y)
if (x>y) x=x-y;
else y=y-x;
return x;
}

int main()
{
int n,a[100],b;
cout<<"n= "; cin>>n;
for(int i=0;i<n;i++)
{cout<<"a["<<i<<"]=";
cin>>a[i];
}
for(i=1;i<n;i++)
if (i==1) b=cmmdc(a[I -1], a[i]);
else b=cmmdc(b, a[i]);
cout<<b;
}

You might also like