A Program to Calculate the LCM of Two Integers

In this example, we are giving a program to calculate the LCM(Lowest common multiple) of given two integers.


Program to calculate the LCM:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int n1, n2, i, gcd, lcm;
  5. printf("Enter two positive integers: ");
  6. scanf("%d %d",&n1,&n2);
  7. for(i=1; i <= n1 && i <= n2; ++i)
  8. {
  9. if(n1%i==0 && n2%i==0)
  10. gcd = i;
  11. }
  12. lcm = (n1*n2)/gcd;
  13. printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);
  14. return 0;
  15. }

Output:

Enter two positive integers: 12
15
The LCM of 12 and 15 is 60.