In this C-program we will calculate the LCM of two numbers. This is the easiest way to calculate LCM for both Negative and Positive numbers. The numbers will be taken from the user.color>
input:
The numbers.(i.e : 20,25,40 etc.)output:
The LCM of two numbers given by the user.CODE---->
#include
#include
{
int copy_num_1,copy_num_2,num_1,num_2,gcd,lcm=0,i ;
printf("Please, Enter two numbers : color>");
scanf("%d%dcolor>",&num_1,&num_2);
copy_num_1=num_1;
copy_num_2=num_2;
//copying numbers , (entered by user) for future use.color>
if(num_1)
num_1=(num_1)*(-1);
if(num_2)
num_2=(num_2)*(-1);
//here, we are converting negative numbers(if any, entered by user), so that, we can calculate the lcm for both positive and negative numbers.color>
for(i=1color>;i &&color> i;i++color>)
{
if(num_1%i==0color> && color>num_2%i==0color>)
gcd=i;
}
lcm=num_1*num_2/gcd;
//relation between lcm and gcd. so, we can get the lcm using this formula.color>
printf("\nThe LCM of %d and %d is : %dcolor>",copy_num_1,copy_num_2,lcm);
getch();
}
color>