C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Saturday, December 28, 2013

Program Find LCM of Two Numbers

Write a C++ Program to input two integer numbers
and Find LCM.

First of all, here are some basic concepts about LCM and the program logic explaining which statements will be used to solve this problem in C++

Definition of LCM of Two Numbers

The Least Common Multiple of two integers a and b is the smallest positive integer that is divisible by both a and b.
LCM-two-numbers-Program-c

Explanation of LCM

A multiple of a number is the product of that number and an integer. For example, 10 is a multiple of 5 because 5 × 2 = 10.

Example: Find the least common multiple for 3 and 5:


The multiples of 3 are 3, 6, 9, 12, 15, and so on
and the multiples of 5 are 5, 10, 15, 20, and so on
Note that the first common multiple is 15. Therefore, LCM of 3 and 5 is 15 because it is the least common multiple.
Program-find-LCM-two-numbers

Example: Find the Least Common Multiple of 4 and 6?


We write down Multiples of 4 :

    4, 8, 12, 16, 20, 24, 28, 32, 36, 40, ...

and we write down the multiples of 6 :

    6, 12, 18, 24, 30, 36, 42, 48, 54, 60, ...

Here we write down the Common multiples of 4 and 6::

    12, 24, 36, ....

So, it is clear that 12  is the required least common multiple of 4 and 6.

Definition of LCM of Multiple Numbers

The LCM of more than two integers is the smallest integer that is divisible by each of them.


Program Logic For Finding LCM of Two Numbers

 
Easyway C++ Program LCM Logic and Source Code
Easyway C++ Program LCM Logic

  1. First of all we will input two numbers say a and b, with the help of  cout and cin.
  2. Now we check if any one number is 1 then LCM is 1.
  3. If both numbers are greater than 1 then we initialize a loop counter by 2.
  4. Now we start a for loop from 2 to onward until we find a number that is divisible by both numbers a and b. Note that we have no condition expression in for loop. Only initialization and increment expression (increment loop counter by 1) is used in for loop. So, this is an infinite loop. [Note that such loops may be terminated by a break statement upon satisfying some condition.]
  5. In Loop body, we check that if current number may divide a and b both? If so, then this current number is the LCM of a and b. And after displaying the LCM we will use a break statement to terminate the loop.

Program Code: Find LCM of Two Numbers

/*
Write a C++ Program to input two integer numbers
and Find LCM.

For Free C++ Programming Techniques
(Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

void main()
{

  clrscr();

  int a, b, counter;

  cout<<"\n Enter the first number = ";

  cin>>a;

  cout<<"\n Enter the second number = ";

  cin>>b;

  if ( a == 1 || b == 1)
      {
      cout<<"\n LCM of "<<a<<" and "<<b<<" is 1";
      exit(0);
      }

  for(counter = 2; ;counter++ )

    if ( counter % a == 0 && counter % b == 0 )

         {
         cout<<"\n LCM of "<<a<<" and "<<b<<" is "<<counter;
         break;
         }

  getch();

}
Share:

2 comments:

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Blog Archive

Labels