This is a C Program to find GCD and LCM of given two numbers. GCD is calculated using Euclidean Algorithm.

Here is source code of the C Program to Find the GCD and LCM of n Numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`#include <stdio.h>`

`#include <string.h>`

`#include <stdlib.h>`

int gcd(int x, int y) {

int r = 0, a, b;

a = (x > y) ? x : y; // a is greater number

b = (x < y) ? x : y; // b is smaller number

r = b;

while (a % b != 0) {

r = a % b;

a = b;

b = r;

`}`

return r;

`}`

int lcm(int x, int y) {

int a;

a = (x > y) ? x : y; // a is greater number

while (1) {

if (a % x == 0 && a % y == 0)

return a;

++a;

`}`

`}`

int main(int argc, char **argv) {

printf("Enter the two numbers: ");

int x, y;

scanf("%d", &x);

scanf("%d", &y);

printf("The GCD of two numbers is: %d", gcd(x, y));

printf("The LCM of two numbers is: %d", lcm(x, y));

return 0;

`}`

Output:

$ gcc GCDLCM.c $ ./a.out Enter the two numbers: 12 15 The GCD of two numbers is: 3 The LCM of two numbers is: 60

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

advertisement

advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Practice Computer Science MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Buy Computer Science Books
- Buy C Books