LCM program in c with multiple numbers

Posted By: Matpal Media - 1:56 AM
#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;
}

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.