The LCM of two integers n1 and n2 is the smallest positive integer that is perfectly divisible by both n1 and n2 (without a remainder)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | BEGIN NUMBER n1 , n2 , lcm; OUTPUT "Enter first Number:"; INPUT n1 OUTPUT "Enter second Number:"; INPUT n2 lcm = (n1 > n2) ? n1 : n2; WHILE (true) THEN IF (lcm % n1 == 0 && lcm % n2 == 0) THEN OUTPUT "The LCM of "+n1+"and "+n2+" is "+ lcm; BREAK WHILE; END IF ++lcm; END WHILE END |
This code calculates the least common multiple (LCM) of two integers. The LCM is the smallest positive integer that is a multiple of both integers. Here is a brief explanation of how it works:
You may also like: Pseudocode Examples
- The program declares three variables: n1, n2, and lcm.
- The program prompts the user to enter two integers and stores them in n1 and n2.
- The program initializes the lcm variable to the larger of the two integers (n1 and n2).
- The program enters an infinite loop.
- Inside the loop, the program checks whether lcm is a multiple of both n1 and n2. If it is, the program prints out the LCM and breaks out of the loop. If it is not, the program increments lcm by 1 and continues the loop.
- The program ends.
Flowchart