How to find this GCD and LCM

101 Views Asked by At

Im trying to find this GCD and LCM in C

GCD example :

Input 1 : 20

Input 2 : 15

Num1 : 20 = 2 x 2 x 5

Num2 : 15 = 3 x 5

GCD = 5

and

LCM example :

Input 1 : 20

Input 2 : 15

15 : 15, 30, 45, 60, 75, 90, 105, 120, 135, ...

20 : 20, 40, 60, 80, 100, 120, 140, ...

Common Multiple = 60, 120, ...

LCM = 60

How do i print

Num1 : 20 = 2 x 2 x 5

Num2 : 15 = 3 x 5

inside

#include <stdio.h>
int main()
{
    int n1, n2, i, gcd;

    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);

    for(i=1; i <= n1 && i <= n2; ++i)
    {
        
        if(n1%i==0 && n2%i==0)
            gcd = i;
    }

    printf("G.C.D of %d and %d is %d", n1, n2, gcd);

    return 0;
}

and

print

15 : 15, 30, 45, 60, 75, 90, 105, 120, 135, ...

20 : 20, 40, 60, 80, 100, 120, 140, ...

Common Multiple = 60, 120, ...

inside

#include <stdio.h>

int main() {

    int n1, n2, max;

    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);

    max = (n1 > n2) ? n1 : n2;

    while (1) {
        if ((max % n1 == 0) && (max % n2 == 0)) {
            printf("The LCM of %d and %d is %d.", n1, n2, max);
            break;
        }
        ++max;
    }
    return 0;
}

0

There are 0 best solutions below