Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, March 13, 2023

QBASIC program to find the LCM of input two numbers.

 QBASIC program to find the LCM of input two numbers.

 


Introduction:

The Least Common Multiple (LCM) is an important mathematical concept used in various fields such as mathematics, computer science, and engineering. LCM is defined as the smallest positive integer that is a multiple of two or more given numbers. In this article, we will discuss how to write a QBASIC program to find the LCM of input two numbers.
 
Approach:

To find the LCM of two or more numbers, we need to follow the following approach:
i) Find the maximum of the two numbers.
ii) Check if the maximum is a multiple of both numbers.
iii) If yes, then the maximum is the LCM.
iv) If not, then increment the maximum by the original maximum and check if it is a multiple of both numbers.
v) Repeat step 4 until the LCM is found.
 
QBASIC program to find the LCM of input two numbers: Here's the QBASIC program to find the LCM of two input numbers:
 
Without using Procedure
CLS
INPUT "Enter the first number: "; n1
INPUT "Enter the second number: "; n2
REM find the maximum of the two numbers
IF n1 > n2 THEN
max = n1
ELSE
max = n2
END IF
REM loop through multiples of the maximum number until a common multiple is found
i = max
DO WHILE (i MOD n1 <> 0) OR (i MOD n2 <> 0)
i = i + max
LOOP
PRINT "The LCM of"; n1; "and"; n2; "is"; i
END
 
Explanation:
 
  • The CLS command clears the screen.
  • The INPUT statements prompt the user to enter the two numbers.
  • The IF statement finds the maximum of the two numbers and assigns it to the variable max.
  • The DO WHILE loop checks each multiple of max until a number is found that is both a multiple of num1 and num2. This number is assigned to the variable i.
  • Finally, the program prints out the LCM of num1 and num2 by printing out the value of i.
 
 
Using Sub procedure
DECLARE SUB lcm ()
CLS
CALL lcm
END
 
SUB lcm
CLS
INPUT "Enter first number: "; n1
INPUT "Enter second number: "; n2
IF n1 > n2 THEN
max = n1
ELSE
max = n2
END IF
i = max
DO WHILE (i MOD n1 <> 0) OR (i MOD n2 <> 0)
i = i + max
LOOP
PRINT "The LCM of"; n1; "and"; n2; "is"; i
END SUB
 
Using Function Procedure
DECLARE FUNCTION lcm (n1, n2)
CLS
INPUT "Enter first number: "; n1
INPUT "Enter second number: "; n2
PRINT "The LCM of"; n1; "and"; n2; "is"; lcm(n1, n2)
END
 
FUNCTION lcm (n1, n2)
IF n1 > n2 THEN
max = n1
ELSE
max = n2
END IF
i = max
DO WHILE (i MOD n1 <> 0) OR (i MOD n2 <> 0)
i = i + max
LOOP
lcm = i
END FUNCTION
 
Output:
 


Conclusion:

In this article, we have discussed how to write a QBASIC program to find the LCM of input two numbers. The approach we have discussed can be extended to find the LCM of more than two numbers by finding the LCM of the first two numbers, and then finding the LCM of the result and the next number, and so on.


Also Read:

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages