0% found this document useful (0 votes)
74 views2 pages

Program For GCD of N Numbers

This program finds the greatest common divisor (GCD) of n numbers entered by the user. It takes n numbers as input, sorts them in descending order, then iteratively applies the GCD algorithm to pairs of numbers starting from the largest two, storing the results back in the array. It ultimately displays the GCD, which will be the first element of the sorted array.

Uploaded by

Nitish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views2 pages

Program For GCD of N Numbers

This program finds the greatest common divisor (GCD) of n numbers entered by the user. It takes n numbers as input, sorts them in descending order, then iteratively applies the GCD algorithm to pairs of numbers starting from the largest two, storing the results back in the array. It ultimately displays the GCD, which will be the first element of the sorted array.

Uploaded by

Nitish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program for GCD of n numbers

In gcc
#include <iostream>
#include <stdio.h>
#include <conio>
using namespace std;
void algorithm(int *);
int main()
{
int a,b,c,a1[2],a2[100],n;
cout<<"Enter how many numbers u want to enter "<<endl;
cin>>n;
cout<<endl;
for(c=0;c<n;c++)
{
cout<<"Enter a number : "<<endl;
cin>>a2[c];
}
for(a=0;a<(n-1);a++)
{
for(b=a+1;b<n;b++)
{
if(a2[a]>a2[b])
{
c=a2[a];
a2[a]=a2[b];
a2[b]=c;
}
}
}
for(a=n-1;a>0;a--)
{
a1[0]=a2[a];
a1[1]=a2[a-1];
while(1)
{
algorithm(&a1[0]);
if(a1[1]==0)
{
a2[a-1]=a1[0];
a2[a]=0;
break;
}
}
}

cout<<"\nThe GCD = "<<a2[0]<<endl;


return 0;
getch();
}
void algorithm(int *a1)
{
int a,b;
a=*a1;
b=*(a1+1);
cout<<a<<" "<<b<<endl;
*a1 =b;
a1++;
*a1=a%b;
}

You might also like