C++ program to find LCM of two numbers using functions

By | 25.12.2016

LCM of two numbers using functions


Write a C++ program to find LCM of two numbers using functions. Here’s simple program to find LCM of two numbers using functions in C++ Programming Language.


A common multiple is a number that is a multiple of two or more numbers. The common multiples of 3 and 4 are 0, 12, 24, ….

The least common multiple (LCM) of two numbers is the smallest number (not zero) that is a multiple of both.

Here is source code of the C++ program to find LCM of two numbers using functions. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/*  C++ program to find LCM of two numbers using functions  */

#include<iostream>
using namespace std;

void lcm(int,int);

int main()
{
    int a,b;

    cout<<"Enter 1st number :: ";
    cin>>a;
    cout<<"\nEnter 2nd number :: ";
    cin>>b;

    lcm(a,b);

  return 0;
}

//function to calculate l.c.m
void lcm(int a,int b)
{
    int m,n;

    m=a;
    n=b;

    while(m!=n)
    {
        if(m < n)
        {
        m=m+a;
        }
        else
        {
            n=n+b;
            }
    }

    cout<<"\nL.C.M of [ "<<a<<" ] and [ "<<b<<" ] is :: "<<m<<"\n";
}

Output : :


/*  C++ program to find LCM of two numbers using functions  */

Enter 1st number :: 2

Enter 2nd number :: 15

L.C.M of [ 2 ] and [ 15 ] is :: 30

Process returned 0

Above is the source code for C++ program to find LCM of two numbers using functions which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

5 4 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dave

#include <iostream>
using namespace std;

int LCM(int a, int b){
int a1, m, i=2;

while(m!=0){
a1=a*i;
m=a1%b;
i++;
}

return a1;

}

int main(){
int a, b;

cout << Enter number 1: ; //El usuario ingresa una cifra
cin >> a;

cout << Enter number 2: ; // El usuario ingresa otra cifra
cin >> b;

cout << LCM(a,b);

}

Nabeel Tiwana

#include <bits/stdc++.h>

  using namespace std;

  int lcm(int a,int b)
  {
      int ans,max;
      max=(a>b)?a:b;
      for(int i=max;i<=a*b;i+=max)
      {
        if((i%a==0)&&(i%b==0))
        {
          ans=i;
          break;
        }

      }
      return ans;
  }

  int main()
  {
    int a,b;
    cout<<“Enter first number:”;
    cin>>a;
    cout<<“Enter second number:”;
    cin>>b;
    cout<<lcm(a,b);
    cout<<endl;

    return 0;
  }