lcm of two numbers

Code Example - lcm of two numbers

                
                        #include <stdio.h>
int main() {
    int n1, n2, max;
    printf("Enter two positive integers: ");
    scanf("%d %d", &n1, &n2);

    // maximum number between n1 and n2 is stored in max
    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;
}
                    
                
 

Lcm of numbers

                        
                                // call this function
    public static int LCM(List<int> input) 
    {
         int result = input[0];
         for (int i = 1; i < input.Count; i++) 
         {
            result = lcm(result, input[i]);
         }
         return result;
    }

    private static int LCM(int a, int b) 
    {
        return a * (b / gcd(a, b));
    }
	private static int gcd(int a, int b) 
    {
        while (b > 0) 
        {
            int temp = b;
            b = a % b; // % is remainder
            a = temp;
        }
        return a;
    }

    private static int gcd(List<int> input) 
    {
        int result = input[0];
        for (int i = 1; i < input.Count; i++) 
        {
            result = gcd(result, input[i]);
        }
        return result;
    }
                            
                        
 

Related code examples