Tuesday, August 5, 2014

Calculate L.C.M of two numbers.

Write a program to calculate L.C.M of two numbers.

SOLUTION:

 /*
 * Calculate L.C.M of two numbers.
 * Written by Shaeed Khan. 
 * Date: 05 Aug 2014
 * Version- 1.0 
 * Language: C
 */

#include < stdio.h >    
#include < stdlib.h > 

void main()
{
 int n1,n2,x;
 printf("Enter two numbers:");
 scanf("%d%d",&n1,&n2);
 x=lcm(n1,n2);
 printf("%d ",x);
}

int lcm(int n1,int n2)
{
 int x,y;
 x=n1,y=n2;
 while(n1!=n2)
 {
  if(n1>n2)
   n1=n1-n2;
  else
   n2=n2-n1;
 }
 return x*y/n1;
} 

No comments:

Post a Comment