PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to find LCM of two number

Input - Output

Input
Enter two number to find LCM : 12 15

Output
LCM of 12 and 15 : 60

Algorithm

Step 1: START

Step 2: declare vaiable num1 , num2, HCF(to store our result).

Step 3: Input two number from user and store into num1 and num2.

Step 4: Now, we have to find a smallest number from that num1 and num2 divide exactly.

Step 5: LCM = product of (num1 * num 2) / divide by smallest Number(HCF)

Step 5: END.

CODE


/* Find LCM of two number */

#include<stdio.h>
int main()
{
      int num1,num2,HCF,LCM;
      printf(" Enter the two number to find LCM : ");
      scanf("%d%d",&num1,&num2);
      for(int i=1;i<=num1 && i<=num2;i++)
        if(num1 % i == 0 && num2 % i == 0)
        HCF = i;
        LCM = (num1 * num2)/HCF;
        printf(" LCM of %d and %d : %d",num1,num2,LCM);
    return 0;
}


/* Find LCM of two number */

#include<iostream>
using namespace std;
int main()
{
      int num1,num2,HCF,LCM;
      cout<<" Enter the two number to find LCM : ";
      cin>>num1>>num2;
      for(int i=1;i<=num1 && i<=num2;i++)
        if(num1 % i == 0 && num2 % i == 0)
        HCF = i;
        LCM = (num1 * num2)/HCF;
        cout<<" LCM of "<<num1<<" and "<<num2<<" : "<<LCM;
    return 0;
}

OutPut

Enter two number to find LCM : 12 15

LCM of 12 and 15 : 60