C program to find LCM of any two numbers

Write a C program to enter two numbers and find LCM (Lowest Common Multiple) using for loop. How to find LCM of two given numbers in C programming. Find LCM of two numbers using loops in C program.

Input num1: 12
Input num2: 30
Output LCM: 60

Also check this program using recursive approach - C program to find LCM using recursion.

Required knowledge

Basic C programming, If else, Conditional operator, For loop

LCM (Lowest Common Multiple)

LCM is the smallest positive integer that exactly divides two or more numbers.
For Example:
LCM of two numbers
LCM of two numbers

Program to find LCM

 * C program to find LCM of any two numbers

#include <stdio.h>

int main()
    int i, num1, num2, max, lcm=1;

     * Reads two numbers from user
    printf("Enter any two numbers to find LCM: ");
    scanf("%d %d", &num1, &num2);

    max = (num1>num2) ? num1 : num2;

    i = max;
    //Loop runs forever
        /* If i is a multiple of both numbers */
        if(i%num1==0 && i%num2==0)
            lcm = i;

        i += max;

    printf("\nLCM of %d and %d = %d\n", num1, num2, lcm);

    return 0;

Enter any two numbers to find LCM: 12

LCM of 12 and 30 = 60

Happy coding ;)

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment