Google Search

Sunday, February 14, 2016

LCM program in c with multiple numbers

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

No comments:

Post a Comment