C Program to Find LCM of Two Numbers

In this tutorial, i am going to show you how to find the lcm of two numbers with the help of for loop, while loop, recursion and gcd.

All C Programs to find LCM of Two Numbers

  • C Program to find LCM of Two Numbers using While Loop
  • C Program to find LCM of Two Numbers using For Loop
  • C Program to find LCM of Two Numbers using GCD
  • C Program to find LCM of Two Numbers using Recursion

C Program to find LCM of Two Numbers using While Loop

#include <stdio.h>
int main()
{
    int Num1, Num2, max_Value;
    printf("Please Enter two integer Values \n");
    scanf("%d %d", &Num1, &Num2);
    
    max_Value = (Num1 > Num2)? Num1 : Num2;
    
    while(1) //Alway True
    {
    	if(max_Value % Num1 == 0 && max_Value % Num2 == 0) 
    	{
    		printf("LCM of %d and %d = %d", Num1, Num2, max_Value);
    		break;
		}
		++max_Value;
	}
    return 0;
}

The result of the above c program; as follows:

Please Enter two integer Values :- 10 20
LCM of 10 and 20 = 20

C Program to find LCM of Two Numbers using For Loop

#include<stdio.h>
void main()
{
 int a,b,i,max,lcm;
 
  	printf("Enter the two numbers :- ");
 scanf("%d%d",&a,&b);
    
    max=a>b?a:b;
    for(i=0;i<max;i++)
    {
        if(max%a==0 && max%b==0)
        {
            lcm=max;
            break;
        }
        max++;
    }
    
    printf("\nLCM of the two numbers = %d",lcm);
 }

The result of the above c program; as follows:

Enter the two numbers :- 50 60
LCM of the two numbers = 300

C Program to find LCM of Two Numbers using GCD

#include <stdio.h>
int main()
{
    int Num1, Num2, LCM, Temp, GCD;
    printf("Please Enter two integer Values :- ");
    scanf("%d %d", &Num1, &Num2);
    int a = Num1;
    int b = Num2;
    
    while (Num2 != 0) {
 	Temp = Num2;
 	Num2 = Num1 % Num2;
 	Num1 = Temp;
    }
    GCD = Num1;
    printf("GCD of %d and %d = %d \n", a, b, GCD);
    
    LCM = (a * b) / GCD;
    printf("LCM of %d and %d = %d", a, b, LCM);
    return 0;
}

The result of the above c program; as follows:

Please Enter two integer Values :- 50 100
GCD of 50 and 100 = 50 
LCM of 50 and 100 = 100

C Program to find LCM of Two Numbers using Recursion

#include <stdio.h>
long gcd(long x, long y);
int main()
{
    int Num1, Num2, GCD, LCM;
    printf("Please Enter two integer Values \n");
    scanf("%d %d", &Num1, &Num2);
    GCD = gcd(Num1, Num2);
    LCM = (Num1 * Num2) / GCD;
    printf("LCM of %d and %d is = %d", Num1, Num2, LCM);
    return 0;
}
long gcd(long x, long y) 
{
  if (y == 0) {
  	return x;
  }
  else {
    return gcd(y, x % y);
  }
}

The result of the above c program; as follows:

Please Enter two integer Values :- 50 60
LCM of 50 and 60 is = 300

More C Programming Tutorials

Leave a Comment