Programming Example
Find the LCM of two Numbers using Command Line Language
Find the LCM of two Numbers using Command Line Language
#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;
}
10 12
L.C.M of 10 & 12 = 60
#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; }
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.