Loading...

HCF and LCM C Program


C Program of finding HCF and LCM:

#include <stdio.h>
main()
{
    int a, b, x, y, t, hcf, lcm;

    printf("Enter two integers: ");
    scanf("%d%d", &x, &y);

    a = x;
    b = y;

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

    hcf = a;
    lcm = (x*y)/hcf;

    printf("Greatest common divisor of %d and %d = %d\n", x, y, hcf);
    printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
}

Also by Making Function of it:

#include <stdio.h>
void hcfandlcm(int,int);
main()
{
    int x,y;

    printf("Enter two integers: ");
    scanf("%d%d", &x, &y);

    hcfandlcm(x,y);
}
void hcfandlcm(int x,int y)
{
    int a,b,t,hcf,lcm;

    a = x;
    b = y;

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

    hcf = a;
    lcm = (x*y)/hcf;

    printf("Greatest common divisor of %d and %d = %d\n", x, y, hcf);
    printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
}

Rate this posting:
{[['']]}

0 comments:

Post a Comment

 
TOP