# C Exercises: Determine the LCM of two numbers using HCF

## C For Loop: Exercise-44 with Solution

Write a C program to find the LCM of any two numbers using HCF.

**Visual Presentation:**

**Sample Solution:**

**C Code:**

```
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int i, n1, n2, j, hcf = 1, lcm; // Declare variables to store input and results.
printf("\n\n LCM of two numbers:\n "); // Print a message.
printf("----------------------\n"); // Print a separator.
printf("Input 1st number for LCM: "); // Prompt the user for input.
scanf("%d", &n1); // Read the first number from the user.
printf("Input 2nd number for LCM: "); // Prompt the user for input.
scanf("%d", &n2); // Read the second number from the user.
j = (n1 < n2) ? n1 : n2; // Determine the smaller of the two numbers.
// Loop to find the highest common factor (HCF).
for(i = 1; i <= j; i++)
{
if(n1 % i == 0 && n2 % i == 0)
{
hcf = i; // Update the HCF whenever a common factor is found.
}
}
// Calculate the least common multiple (LCM).
lcm = (n1 * n2) / hcf;
// Print the result.
printf("\nThe LCM of %d and %d is : %d\n\n", n1, n2, lcm);
}
```

Sample Output:

LCM of two numbers: ---------------------- Input 1st number for LCM: 15 Input 2nd number for LCM: 20 The LCM of 15 and 20 is : 60

**Flowchart: **

**C Programming Code Editor:**

**Previous:** Write a C program to find HCF (Highest Common Factor) of two numbers.

**Next:** Write a program in C to find LCM of any two numbers.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join