🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
The Least Common Multiple (LCM) of two integers is the smallest integer that is divisible by both of them. The LCM is frequently used in problems related to number theory, fractions, and more. This guide will demonstrate how to write a C program to find the LCM of two numbers.
2. Program Overview
1. Prompt the user to input two numbers.
2. Use the formula: LCM(a, b) = (a * b) / GCD(a, b) to determine the LCM.
3. Display the LCM of the two input numbers.
3. Code Program
#include <stdio.h>
// Function to find the GCD of two numbers
int gcd(int a, int b) {
while(b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to find the LCM of two numbers
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
// Asking user to input the numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Printing the LCM
printf("LCM of %d and %d is: %d", num1, num2, lcm(num1, num2));
return 0;
}
Output:
Enter two numbers: 15 20 LCM of 15 and 20 is: 60
4. Step By Step Explanation
1. The program starts by defining a function gcd that calculates the Greatest Common Divisor of two numbers.
2. A second function lcm is defined to compute the LCM using the relationship: LCM(a, b) = (a * b) / GCD(a, b).
3. In the main function, the user is prompted to input two numbers.
4. The LCM of the two input numbers is then computed by calling the lcm function.
5. The LCM is then displayed.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment