Q17 Write a function to compute the greatest common divisor given by Euclid’s algorithm, exemplified for J=1980, K=1617 as follows: Thus, the greatest common divisor is 33.

Write a function to compute the greatest common divisor given by Euclid's algorithm, exemplified for J=1980, K=1617 as follows: Thus, the greatest common divisor is 33.

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

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.