Tuesday, April 6, 2021

18.16. Write a function that gets two positive integers and returns LCM (least common multiple).

#include<stdio.h>
#include<conio.h>
int LCM(int a, int b)
{

int i,l=1;
while(a%i!==0 && b%i==0)
for(i=2;i<=a && i<=b;i++)
{
{

a=a/i;
b=b/i;
l=l*i;
}
}
l=l*a*b;
return l;
}
void main ()
{
int a,b;
clrscr();
printf("Enter any positive integer: ");
scanf ("%d", &a);
printf("Enter any positive integer: ");
scanf ("%d", &b);
printf("
LCM of %d=%d", LCM(a,b)) ;
getch ();
}

No comments:

Post a Comment