

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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("\nThe GCD of %d and %d is %d\n",firstno,secondno,gcd); printf("\nThe LCM of %d and %d is %d\n",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("\nThe GCD of %d and %d is %d\n",firstno,secondno,gcd); printf("\nThe LCM of %d and %d is %d\n",firstno,secondno,(firstno*secondno)/gcd); } else printf("One of the entered no. is zero:Quitting\n"); } /*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 Questions & Answers
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Dijkstra’s Algorithm Using Set
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Modular Exponentiation Algorithm
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement the Bin Packing Algorithm
- C++ Program to Implement The Edmonds-Karp Algorithm
- C++ Program to Implement Levenshtein Distance Computing Algorithm
- C++ Program to Implement Booth’s Multiplication Algorithm for Multiplication of 2 signed Numbers
Advertisements