
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 −
#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
- Related Articles
- What is Euclid's division algorithm?
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Modular Exponentiation Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- C++ Program to Implement Levenshtein Distance Computing Algorithm
- C++ Program to Implement Dijkstra’s Algorithm Using Set
- C++ Program to Implement The Edmonds-Karp Algorithm
- C++ Program to Implement the Bin Packing Algorithm

Advertisements