Program to find GCD and LCM of 2 numbers

Program to find GCD and LCM of 2 numbers

The GCD or Greatest Common Divisor of 2 or more non-zero integers is the largest positive integer that divides each of them. The LCM or Least Common Multiple of 2 numbers is the smallest number that is a multiple of both numbers. So let’s see the program to find GCD and LCM of 2 numbers.

1. Program in C

#include<stdio.h>
int main() {
  int i, c = 0, n1, n2, rem, numer, den, gcd, lcm;
  printf("\n Enter 2 numbers: ");
  scanf("%d%d", & n1, & n2);
  if (n1 > n2)
    i = n1;
  else
    i = n2;
  while (c == 0) {
    if ((i % n1 == 0) && (i % n2 == 0)) {
      lcm = i;
      c = 1;
    }
    i++;
  }
  c = 0;
  if (n1 > n2)
    i = n1;
  else
    i = n2;
  while ((i > 0) && (c == 0)) {
    if ((n1 % i == 0) && (n2 % i == 0)) {
      gcd = i;
      c = 1;
    }
    i--;
  }
  printf("\n GCD of %d and %d is = %d", n1, n2, gcd);
  printf("\n\n LCM of %d and %d is = %d", n1, n2, lcm);
  return 0;
}

2. Program in C++

#include<iostream>
using namespace std;
int main() {
  int i, c = 0, n1, n2, rem, numer, den, gcd, lcm;
  cout << "\n Enter 2 numbers: ";
  cin >> n1 >> n2;
  if (n1 > n2)
    i = n1;
  else
    i = n2;
  while (c == 0) {
    if ((i % n1 == 0) && (i % n2 == 0)) {
      lcm = i;
      c = 1;
    }
    i++;
  }
  c = 0;
  if (n1 > n2)
    i = n1;
  else
    i = n2;
  while ((i > 0) && (c == 0)) {
    if ((n1 % i == 0) && (n2 % i == 0)) {
      gcd = i;
      c = 1;
    }
    i--;
  }
  cout << "\n GCD of " << n1 << " and " << n2 << " is = " << gcd << endl;
  cout << "\n LCM of " << n1 << " and " << n2 << " is = " << lcm;
  return 0;
}

3. Output-

program to find gcd lcm

4. Explanation-

The logic used in this program is simple. Firstly 2 numbers are taken as input. Then the greater number is assigned to variable ‘i’. Using while loop we check if ‘i’ is divisible completely by both numbers. ‘i‘ is incremented until this condition is satisfied. Thus we get the LCM of numbers. Again the greater number is assigned to variable ‘i’. Another while loop checks if both numbers are divisible completely by ‘i’. This time ‘i’ is decremented until the condition is satisfied. So we get the GCD of numbers. Finally the values are printed.

5. Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. print/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

This was the program to find GCD and LCM in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

1 COMMENT

Leave a Reply

Your email address will not be published. Required fields are marked *