#include<stdio.h>
#include<conio.h>
#define size 100
int Gcd(int *);
int n=0;
int main()
{
int x[size],z=0;
printf("\n for how many values do u want to find gcd????::::");
scanf("%d",&n);
printf("\n enter %d values to find gcd ...",n);
while(z<n)
{
scanf("%d",&x[z]);
z++;
}
x[n]=Gcd(x);
if(x[n]==-1)
{
printf("\n gcd doesn't exist....");
}
else if(x[n]==-2)
{
}
else
{
printf("gcd of given numbers is : %d",x[n]);
}
_getch();
return 0;
}
int Gcd(int *ptr)
{
int a[size],c,d,i=0,j=1,k=0,z=0;
while(k<n)
{
a[k]=*ptr;
ptr++;
k++;
}
while(z<n)
{
if(a[z]==0)
{
printf("\n gcd of the given numbers is : 0");
goto a;
}
z++;
}
while(i<n)
{
while(j<n)
{
if(a[i]>a[j])
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
j++;
}
i++;
}
c=a[0];
d=n-1;
while(c>1)
{
while(a[d]%c==0)
{
if(d>1)
{
d--;
continue;
}
else
return c;
}
c--;
if(c==1)
return -1;
}
a:
return -2;
}
#include<conio.h>
#define size 100
int Gcd(int *);
int n=0;
int main()
{
int x[size],z=0;
printf("\n for how many values do u want to find gcd????::::");
scanf("%d",&n);
printf("\n enter %d values to find gcd ...",n);
while(z<n)
{
scanf("%d",&x[z]);
z++;
}
x[n]=Gcd(x);
if(x[n]==-1)
{
printf("\n gcd doesn't exist....");
}
else if(x[n]==-2)
{
}
else
{
printf("gcd of given numbers is : %d",x[n]);
}
_getch();
return 0;
}
int Gcd(int *ptr)
{
int a[size],c,d,i=0,j=1,k=0,z=0;
while(k<n)
{
a[k]=*ptr;
ptr++;
k++;
}
while(z<n)
{
if(a[z]==0)
{
printf("\n gcd of the given numbers is : 0");
goto a;
}
z++;
}
while(i<n)
{
while(j<n)
{
if(a[i]>a[j])
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
j++;
}
i++;
}
c=a[0];
d=n-1;
while(c>1)
{
while(a[d]%c==0)
{
if(d>1)
{
d--;
continue;
}
else
return c;
}
c--;
if(c==1)
return -1;
}
a:
return -2;
}
No comments:
Post a Comment