# C program to implement Euclid’ s algorithm

## Problem

Implement Euclid’ s algorithm to find the greatest common divisor (GCD) and least common multiple (LCM) of two integers and to output the results along with the given integers.

## Solution

The solution to implement Euclid’ s algorithm to find the greatest common divisor (GCD) and least common multiple (LCM) of two integers is as follows −

The logic used to find GCD and LCM is as follows −

if(firstno*secondno!=0){
gcd=gcd_rec(firstno,secondno);
printf("
The GCD of %d and %d is %d
",firstno,secondno,gcd);    printf("
The LCM of %d and %d is %d
",firstno,secondno,(firstno*secondno)/gcd); }

The called function is as follows −

int gcd_rec(int x, int y){
if (y == 0)
return x;
return gcd_rec(y, x % y);
}

## Program

Following is the C program to implement Euclid’ s algorithm to find the greatest common divisor (GCD) and least common multiple (LCM) of two integers

Live Demo

#include<stdio.h>
int gcd_rec(int,int);
void main(){
int firstno,secondno,gcd;
printf("Enter the two no.s to find GCD and LCM:");
scanf("%d%d",&firstno,&secondno);
if(firstno*secondno!=0){
gcd=gcd_rec(firstno,secondno);
printf("
The GCD of %d and %d is %d
",firstno,secondno,gcd);       printf("
The LCM of %d and %d is %d
",firstno,secondno,(firstno*secondno)/gcd);    }    else       printf("One of the entered no. is zero:Quitting
");    }    /*Function for Euclid's Procedure*/    int gcd_rec(int x, int y){    if (y == 0)       return x;    return gcd_rec(y, x % y); }

## Output

When the above program is executed, it produces the following result −

Enter the two no.s to find GCD and LCM:4 8

The GCD of 4 and 8 is 4

The LCM of 4 and 8 is 8