- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
First, let us learn how to find Highest Common Factor (HCF).
Highest Common Factor (HCF)
The greatest number divides each of the two or more numbers is called HCF or Highest Common Factor. It is also called as Greatest Common Measure(GCM) and Greatest Common Divisor(GCD).
For example,
What is the HCF of 12 and 16?
Factors of 12 = 1, 2, 3, 4, 6,12. Factors of 16=1,2,4,8,16
Highest common factor (H.C.F) of 12 and 16 = 4.
Least Common Multiple (LCM)
For two integers x and y, denoted LCM(x,y), it is the smallest positive integer that is divisible by both x and y.
For example,
LCM(2,3) = 6 and LCM(6,10) = 30.
Example
#include <stdio.h> int main() { int num1, num2, x, y, temp, gcd, lcm; printf("Enter two integers
"); scanf("%d%d", &x, &y); num1 = x; num2 = y; while (num2 != 0) { temp = num2; num2 = num1 % num2; num1 = temp; } gcd = num1; lcm = (x*y)/gcd; printf("GCD of %d and %d = %d
", x, y, gcd); printf("LCM of %d and %d = %d
", x, y, lcm); return 0; }
Output
Upon execution, you will receive the following output −
Run 1: Enter two integers 6 12 GCD of 6 and 12 = 6 LCM of 6 and 12 = 12 Run 2: Enter two integers 24 36 GCD of 24 and 36 = 12 LCM of 24 and 36 = 72
Advertisements