🎓 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
LCM (Least Common Multiple) and GCD (Greatest Common Divisor) are fundamental mathematical operations often used in number theory. In this post, we'll explore how to compute the LCM and GCD of two numbers using recursion in the C programming language.
2. Program Overview
The program will:
1. Define recursive functions for calculating the GCD.
2. Use the relationship between LCM and GCD to compute LCM: lcm(a,b) = (a * b) / gcd(a,b).
3. Get two numbers from the user.
4. Calculate and display the GCD and LCM of the entered numbers.
3. Code Program
#include <stdio.h>
// Recursive function to return GCD of a and b
int gcd(int a, int b) {
if (b == 0) // Base case
return a;
return gcd(b, a % b); // Recursive case
}
// Function to return LCM of two numbers
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
// Getting numbers from the user
printf("Enter two integers: ");
scanf("%d%d", &num1, &num2);
// Displaying the results
printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
printf("LCM of %d and %d is %d\n", num1, num2, lcm(num1, num2));
return 0;
}
Output:
For input numbers 12 and 15, the program would output: GCD of 12 and 15 is 3 LCM of 12 and 15 is 60
4. Step By Step Explanation
1. The program begins with the definition of the gcd function. This function uses Euclid's algorithm to find the GCD of two numbers:
- If b is 0, return a (base case).- Otherwise, recursively call the gcd function with arguments b and a % b.
2. The lcm function calculates the LCM using the relationship between LCM and GCD: lcm(a,b) = (a * b) / gcd(a,b). This formula takes advantage of the fact that the product of two numbers is equal to the product of their LCM and GCD.
3. In the main function, the program gets two integer inputs from the user.
4. The GCD and LCM of these numbers are then calculated using the gcd and lcm functions respectively.
5. Finally, the program displays the computed GCD and LCM of the input numbers.
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