QBASIC program to enter any three numbers and display the greatest number
Introduction
In the world of computer programming, QBASIC has been one of the most popular languages for decades. It is a high-level language that is used to write programs that are easy to read and understand. In this article, we will discuss how to write a QBASIC program to enter any three numbers and display the greatest number.
Getting Started
Before we begin, you need to have a basic understanding of QBASIC. QBASIC is an interpreter language, which means that it runs programs directly from the source code without the need for a compiler. You can download QBASIC from various online sources. Once you have QBASIC installed on your system, you can begin writing your program.
Coding for QBASIC Program to Enter Any Three Numbers and Display the Greatest Number
CLS
INPUT "Enter the first number: ", Num1
INPUT "Enter the second number: ", Num2
INPUT "Enter the third number: ", Num3
IF Num1 > Num2 AND Num1 > Num3 THEN
PRINT "The greatest number is "; Num1
ELSEIF Num2 > Num1 AND Num2 > Num3 THEN
PRINT "The greatest number is "; Num2
ELSE
PRINT "The greatest number is "; Num3
ENDIF
END
Explanation of the Code:
Next, we have an IF statement that checks which of the three numbers is the greatest. The logical operator used here is AND. The condition in the IF statement is true only if both of the conditions separated by the AND operator are true.
The first condition is Num1 > Num2, which checks whether the first number entered is greater than the second number. The second condition is Num1 > Num3, which checks whether the first number entered is greater than the third number. If both of these conditions are true, then the first number entered is the greatest number, and the program displays "The greatest number is" followed by the value of Num1.
The next condition is an ELSEIF statement. If the first condition is false, then the program checks whether the second number is the greatest number. If Num2 is greater than both Num1 and Num3, then the program displays "The greatest number is" followed by the value of Num2.
If both of the above conditions are false, then the program moves to the ELSE statement, where it displays "The greatest number is" followed by the value of Num3.
Finally, the program ends with the END statement.
Example: QBASIC program to enter any three numbers and display the greatest number by
using SUB procedure (SUB…END SUB)
DECLARE
SUB gr ()
CLS
CALL
gr
END
SUB
gr
INPUT
"Enter first number:"; a
INPUT
"Enter second number:"; b
INPUT
"Enter third number:"; c
IF
a > c AND a > b THEN
g
= a
ELSEIF
b > c THEN
g
= b
ELSE
g
= c
END
IF
PRINT
"Greatest no:"; g
END
SUB
Example: QBASIC program to enter any three numbers and display the greatest number by
using FUNCTION procedure
DECLARE
FUNCTION gr (a, b, c)
CLS
INPUT
"Enter first number:"; a
INPUT
"Enter second number:"; b
INPUT
"Enter third number:"; c
great
= gr(a, b, c)
PRINT
"Greatest no="; great
END
FUNCTION
gr (a, b, c)
IF
a > c AND a > b THEN
g
= a
ELSEIF
b > c THEN
g
= b
ELSE
g
= c
END
IF
gr
= g
END
FUNCTION
Output:
No comments:
Post a Comment