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

🖥 Program 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;
}
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.