Program: 138
Write a C 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
5/33=5 165-5*33=0
Thus, the greatest common divisor is 33.
#include<stdio.h> int gcd(int a, int b); void main() { int a,b; printf("Enter the value of a: "); scanf("%d", &a); printf("Enter the value of b: "); scanf("%d", &b); printf("Greatest Common Divisor of (%d, %d): %d",a,b,gcd(a,b)); } int gcd(int a, int b) { //create a temp variable (t) int t; while(b != 0) { t = b; b = a % b; a = t; } return a; }
Output:
Enter the value of a: 1980 Enter the value of b: 1617 Greatest Common Divisor of (1980, 1617): 33
Leave a Comment