Saturday, May 2, 2015

C program to find GCD of three integers


# include < stdio.h>
int gcd(int,int);
void main()
{
 int x,y,z,gc;
 clrscr();
 printf("\nEnter three numbers:");
 scanf("%d%d%d",&x,&y,&z);
  if(x>y)
 gc=gcd(x,y);
 else
 gc=gcd(y,x);
 if(gc>z)
 gc=gcd(gc,z);
 else
 gc=gcd(z,gc);
 printf("\nGCD of %d, %d and %d =%d",x,y,z,gc);
 getch();
 }
  int gcd(int n,int m)
  {
  int c;
  while(1)
  {
  if(n%m==0)
  break;
  else
  {
  c=n%m;
  n=m;
  m=c;
  }
  }
  return m;
  }

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner