Thursday, September 4, 2014

find the greatest common divisor


Problem

Write a function to compute the greatest common divisor given by Euclid’s algorithm, exemplified for J = 1980, K = 1617 as follows:
1980 / 1617 = 1   1980 – 1 * 1617 = 363
1617 / 363 = 4   1617 – 4 * 363 = 165
363 / 165 = 2   363 – 2 * 165 = 33
165 / 33 = 5   165 – 5 * 33 = 0
Thus, the greatest common divisor is 33.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void gcd();
int main()
{
 gcd();
 system("pause");
 return 0;
}
void gcd()
{
 int j,k,x,y,z,a,b,c,d,e ;
 printf("Enter two numbers of 4 digit : ");
 scanf("%d%d",&j,&k);
 x = j/k ;
 y = j-x*k;
 z = k/y ;
 a = k-z*y;
 b = y/a;
 c = y-b*a;
 d = a/c;
 e = a-d*c;
 printf("The greatest common divisor : %d\n",c);
}

Sample Output

greatest common divisor c language

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact