C Programs to Calculate HCF and LCM of Two Numbers

HCF and LCM are important mathematical concepts used in programming. HCF is the greatest number that divides two numbers exactly, while LCM is the smallest positive number divisible by both numbers.

HCF और LCM गणित की महत्वपूर्ण अवधारणाएँ हैं। HCF वह सबसे बड़ी संख्या है जो दो संख्याओं को पूरी तरह विभाजित करती है, जबकि LCM वह सबसे छोटी धनात्मक संख्या है जो दोनों संख्याओं से विभाजित होती है।

C Program to Calculate HCF:-

Formula

HCF (Highest Common Factor) is calculated using the Euclidean algorithm.

C Program

#include <stdio.h>

int main()
{
    int a, b, divisor, rem;

    printf("Enter two integer numbers: ");
    scanf("%d%d", &a, &b);

    while (b != 0)
    {
        rem = a % b;
        a = b;
        b = rem;
    }

    printf("HCF = %d", a);

    return 0;
}

Sample Output

Enter two integer numbers: 12 18
HCF = 6

Explanation (English)

The program uses the Euclidean algorithm to calculate HCF. It repeatedly divides the first number by the second and updates the values using the remainder. When the remainder becomes zero, the last non-zero divisor is the HCF.

व्याख्या (हिन्दी)

यह प्रोग्राम HCF की गणना के लिए Euclidean Algorithm का उपयोग करता है। इसमें बार-बार भाग देकर शेषफल निकाला जाता है। जब शेषफल शून्य हो जाता है, तब अंतिम गैर-शून्य भाजक HCF होता है।


C Program to Calculate LCM

Formula

LCM = (First Number × Second Number) / HCF

C Program

#include <stdio.h>

int main()
{
    int a, b, x, y, rem, hcf, lcm;

    printf("Enter two integer numbers: ");
    scanf("%d%d", &a, &b);

    x = a;
    y = b;

    while (y != 0)
    {
        rem = x % y;
        x = y;
        y = rem;
    }

    hcf = x;

    if (hcf != 0)
        lcm = (a * b) / hcf;
    else
        lcm = 0;

    printf("LCM = %d", lcm);

    return 0;
}

Sample Output

Enter two integer numbers: 12 18
LCM = 36

Explanation (English)

The program first calculates HCF using the Euclidean algorithm. It then uses the HCF and the given formula to calculate LCM.

व्याख्या (हिन्दी)

यह प्रोग्राम पहले Euclidean Algorithm की सहायता से HCF की गणना करता है। इसके बाद HCF तथा दिए गए सूत्र का उपयोग करके LCM का मान निकालता है।

These programs demonstrate the use of while loops, the modulus operator (%), and mathematical formulas in C programming.

ये प्रोग्राम C भाषा में while लूप, Modulus Operator (%) तथा गणितीय सूत्रों के उपयोग को समझाते हैं।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025