Coding Practice

Write a C program to find LCM of two numbers

Find LCM of two numbers
Sample Output
Find LCM of two numbers.

Enter first number: 12
Enter second number: 30

GCD: 6

LCM: 60
Source Code
#include<stdio.h>

int main()
{
    int dividend, divisor, remainder = 1, lcm, dividendClone, divisorClone;

    printf("Find LCM of two numbers.\n\nEnter first number: ");
    scanf("%d", &dividend);

    printf("Enter second number: ");
    scanf("%d", &divisor);

    dividendClone = dividend;
    divisorClone = divisor;

    while(remainder != 0)
    {
        remainder = dividend % divisor;
        dividend = divisor;
        divisor = remainder;
    }
    printf("\nGCD: %d\n", dividend);
    lcm = (dividendClone * divisorClone) / dividend;

    printf("\nLCM: %d\n", lcm);

    return 0;
}
Sample Output
Find LCM of two numbers.

Enter first number: 120
Enter second number: 72

GCD: 24

LCM: 360

No comments:

Post a Comment

Change Theme
X