LCM of Two Numbers using Command Line Language

Ques. Find the LCM of two Numbers using Command Line Language?

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.

Also, write your version of code in the comments –

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

2 comments on “LCM of Two Numbers using Command Line Language”


  • Mahima Patel

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