Find the LCM of two Numbers using Command Line Language

C Programming Language / Command Line Arguments

633

Program:

#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;
}

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.