Header Ads Widget

C- program to find LCM of two numbers

How to find lcm of two numbers:-
                                                               LCM of two numbers is the maximum number which is greater than or equal to maximum between two numbers .
Example:-  lcm of 12 and 8 is:-
           step 1:-  find maximum between both numbers (which is 12)
           step 2:-  check whether maximum ( 12 ) is divisible of both numbers.
           step 3:-  if  maximum ( 12 ) is divisible by both than return it.
           step 4:-   else   increase the maximum ( 12 ) value by 1 and repeat step 2 and 3 while it is not divisible by both.

 By this lcm of 12 and 8 is 24 because 24 is divisible by both numbers. And before 24 there are no any other number which is divisible by both.

Code:-
#include <stdio.h>

// function for find lcm
int lcm(int a ,int b,int c)
{
if(c%a==0 && c%b==0)
return c;
else
{
c++;
lcm(a,b,c);
}
}
int main()
{
int a,b,x,ans;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
x=a>b?a:b; // find maximum between two numbers if a greater than return a
ans=lcm(a,b,x); // function calling
printf("lcm of %d and %d is %d",a,b,ans);
return 0;
}

Output:-

Enter two numbers
12 56
lcm of 12 and 56 is 168




Post a Comment

2 Comments