C Program to find LCM of two Integers

Introduction

The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems.

In order to understand the code given below you’ve got to have prior knowledge and or understanding of the following topics:

If you have a sound knowledge and understanding of what the functions of the concepts are then there will be no difficulty in understanding the given codes below. With all that said lets get coding!

Why Learn C Programming Language?

Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.

Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.

LCM of Two Integers

The smallest number that is a multiple of all the numbers in a group is called the Least Common Multiple (LCM). For example, the LCM of 16 and 20 is 80, which is the smallest number that is a multiple of both 16 and 20. Several methods exist for determining the LCM of two or more numbers.

The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build a LCM finder of two integers c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.

Method 1: Using while loop and if….else statements to Find LCM

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

int main() 
{
    int num1, num2, minNum;
    printf("Enter your any two positive integer number to find LCM: ");
    scanf("%d %d", &num1, &num2);
    minNum = (num1 > num2) ? num1 : num2;
    while (1) 
    {
        if (minNum % num1 == 0 && minNum % num2 == 0) 
		{
            printf("Here is the  LCM of %d and %d is %d.", num1, num2, minNum);
            break;
        }
        ++minNum;
    }
    return 0;
}

Method 2: Using GCD to find LCM

Check out C Program to find GCD of two Integers to understand the below code.

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

int main() {
    int n1, n2, i, gcd, lcm;
    printf("Enter two positive integers to find LCM from GCD: ");
    scanf("%d %d", &n1, &n2);

    for (i = 1; i <= n1 && i <= n2; ++i) {
        
        // check if i is a factor of both integers or not
        if (n1 % i == 0 && n2 % i == 0)
            gcd = i;
    }

    lcm = (n1 * n2) / gcd;

    printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);
    return 0;
}

Output

  • Output for Method 1 :
  • Output for Method 2 :
SHARE C Program to find LCM of two Integers

You may also like...

Leave a Reply

Your email address will not be published.

Share