LCM program in c with two numbers :
#include<stdio.h>
int main(){
int n1,n2,x,y;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2){
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
return 0;
}
LCM program in c with two numbers (Other logic) :
LCM program in c with multiple numbers :
#include<stdio.h>
int lcm(int,int);
int main(){
int a,b,l;
printf("Enter any two positive integers ");
scanf("%d%d",&a,&b);
if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);
printf("LCM of two integers is %d",l);
return 0;
}
int lcm(int a,int b){
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}
#include<stdio.h>
int lcm(int,int);
int main(){
int a,b=1;
printf("Enter positive integers. To quit press zero.");
while(1){
scanf("%d",&a);
if(a<1)
break;
else if(a>b)
b = lcm(a,b);
else
b = lcm(b,a);
}
printf("LCM is %d",b);
return 0;
}
int lcm(int a,int b){
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}
Definition of LCM (Least common multiple):
LCM of two integers is a smallest positive integer which is multiple of both integers that it is divisible by the both of the numbers.
For example: LCM of two integers 2 and 5 is 10 since 10 is the smallest positive numbers which is divisible by both 2 and 5.
No comments:
Post a Comment