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

Wednesday, March 15, 2023

Qbasic program to find HCF of input two numbers

Qbasic program to find HCF of input two numbers




Introduction:

QBasic is a high-level programming language that is easy to learn and was popular in the 1980s and 1990s. It is a great choice for beginners who are just starting to learn programming. In this article, we will discuss how to write a QBasic program to find the HCF (Highest Common Factor) of input two numbers.

Before we dive into the program, let's first understand what HCF is. HCF is the largest positive integer that divides two or more numbers without leaving a remainder. For example, the HCF of 12 and 18 is 6, because 6 is the largest number that divides both 12 and 18 without leaving a remainder.

Qbasic coding for find the HCF of input two numbers.

CLS
INPUT "Enter first number:"; n1
INPUT "Enter second number:"; n2
IF n1 < n2 THEN
min = n1
ELSE
min = n2
END IF
i = 1
DO WHILE i <= min
IF (n1 MOD i = 0) AND (n2 MOD i = 0) THEN
hcf = i
END IF
i = i + 1
LOOP
PRINT "The HCF of"; n1; "and"; n2; "is"; hcf
END


Using SUB Procedure

DECLARE SUB hcf ()
CLS
CALL hcf
END

SUB hcf
INPUT "Enter first number:"; n1
INPUT "Enter second number:"; n2
IF n1 < n2 THEN
min = n1
ELSE
min = n2
END IF
i = 1
DO WHILE i <= min
IF (n1 MOD i = 0) AND (n2 MOD i = 0) THEN
h = i
END IF
i = i + 1
LOOP
PRINT "The HCF of"; n1; "and"; n2; "is"; h
END SUB


Using Function Procedure

DECLARE FUNCTION hcf (n1, n2)
CLS
INPUT "Enter first number:"; n1
INPUT "Enter second number:"; n2
PRINT "The HCF of"; n1; "and"; n2; "is:"; hcf(n1, n2)
END

FUNCTION hcf (n1, n2)
IF n1 < n2 THEN
min = n1
ELSE
min = n2
END IF
i = 1
DO WHILE i <= min
IF (n1 MOD i = 0) AND (n2 MOD i = 0) THEN
h = i
END IF
i = i + 1
LOOP
hcf = h
END FUNCTION

Output:




Conclusion:

In conclusion, QBasic is a simple and easy-to-learn programming language that can be used to write programs to solve various mathematical problems, including finding the HCF of two input numbers. The program we discussed in this article is a great example of how to write a QBasic program to find the HCF of input two numbers. By using this program as a starting point, beginners can learn the basics of programming and develop their skills in QBasic.


You may also Read:


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages