C Program to Find LCM of Two Numbers

C Program to Find LCM of Two Numbers

C program to find the lcm of two numbers; Through this tutorial, we will learn how to find the lcm of two numbers using for loop, while loop, recursion and gcd.

Programs to find LCM of Two Numbers in C

  • 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 output 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 output 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 output 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 output of the above c program; as follows:

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

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *