Home / Programs / Find the LCM of two Numbers using Command Line Language
Programming Example

Find the LCM of two Numbers using Command Line Language

👁 713 Views
💻 Practical Program
📘 Step by Step Learning
Find the LCM of two Numbers using Command Line Language

Program Code

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
  int n1,n2,x,y;
  if (argc == 1 || argc > 3)
  {
    printf("Enter Two Number\r\n");
    exit(0);
  }
  x=atoi(argv[1]);
  y=atoi(argv[2]);
  n1 = x; n2 = y;
  while(n1!=n2){
      if(n1>n2)
           n1=n1-n2;
      else
      n2=n2-n1;
  }
  printf("L.C.M of %d & %d = %d \r\n",x,y,x*y/n1);
  return 0;
}

Output

10 12
L.C.M of 10 & 12 = 60 



Explanation

#include 
#include 
int main(int* argc, char* argv[])
{
int a, b, i, gcd, n1, n2;
int lcm, lcm1;
if(argc==1)
{
printf(“Not sufficient value provided”);
}
else
{
a = atoi( argv[1] );
b = atoi( argv[2] );
n1 = a;
n2 = b;
for( i = 1 ; i < a && i < b ; i++)
{
if( a % i == 0 && b % i == 0 )
{
gcd=i;
}
}
lcm = (n1*n2)/gcd;
lcm1 = (int)lcm;
printf("%d",lcm1);
}
return 0;
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.