Write a program that asks the user to enter two integers, then calculates and display their greatest common divisor (GCD)

PHOTO EMBED

Mon Oct 07 2024 23:22:29 GMT+0000 (Coordinated Universal Time)

Saved by @khadizasultana #c

#include<stdio.h>
// Author- Khadiza Sultana
// 10/8/2024
// Determing GCD of two integers using loop
int main()
{
    int a, b; // take two integers as input
    printf("Enter the integers:");
    scanf("%d %d", &a, &b);
    while (b!=0)
    {
      int remainder = a % b; //1. Determining the remainder of a/b
      a = b; //2. storing b into a
      b = remainder;//3. storing remainder into b
    }
    printf("The GCD of the two integers is: %d", a); // 'a' holds the GCD after the loop
    return 0;
}
content_copyCOPY

The classic algorithm for computing the GCD, known as Euclid's algorithm, goes as follows: Let m and n be variables containing the two numbers. If n is 0, then stop: m-contains the GCD. Otherwise compute the remainder when m is divided by n. Copy n into m and copy the remainder into n. Then repeat the process, starting with testing whether n is 0.