Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Example - LCM of Two Numbers

C++ Example - LCM of Two Numbers

C++ Example C++ Example

The user enters two numbers; find the least common multiple (LCM) of the two numbers.

Example

include

<

iostream

using

namespace

std

;

int

main

(

)

{

int

n1

,

n2

,

max

;

cout

<<

"

Enter two numbers:

"

;

cin

n1

n2

;

//

Get the larger number

max

=

(

n1

n2

)

?

n1

:

n2

;

do

{

if

(

max

%

n1

==

0

&&

max

%

n2

==

0

)

{

cout

<<

"

LCM =

"

<<

max

;

break

;

}

else

++

max

;

}

while

(

true

)

;

return

0

;

}

The output of the above program is:



输入两个数: 12

18

LCM = 36

Example

include

<

iostream

using

namespace

std

;

int

main

(

)

{

int

n1

,

n2

,

hcf

,

temp

,

lcm

;

cout

<<

"

Enter two numbers:

"

;

cin

n1

n2

;

hcf

=

n1

;

temp

=

n2

;

while

(

hcf

!=

temp

)

{

if

(

hcf

temp

)

hcf

-=

temp

;

else

temp

-=

hcf

;

}

lcm

=

(

n1

n2

)

/

hcf

;

cout

<<

"

LCM =

"

<<

lcm

;

return

0

;

}

The output of the above program is:



输入两个整数: 78

52

HCF = 156

C++ Example C++ Example