BASIC Program to Compare The Greatest of Three Numbers

This program compares three numbers and returns a result on which is the greatest.

10 CLS
20 INPUT "Enter the first number:"; A
30 INPUT "Enter the second number:"; B
40 INPUT "Enter the third number:"; C
50 IF A > B AND A > C THEN 90
60 IF B > A AND B > C THEN 100
70 GOTO 110
90 PRINT "The first number is the greatest:"; A
95 GOTO 130
100 PRINT "The second number is the greatest:"; B
105 GOTO 130
110 PRINT "The third number is the greatest:"; C
130 END

Line 10 clears the screen with the command 'CLS'. Line 20 to 40 will request the 3 numbers from the user. Line 50 to 60 is a conditional statement using IF-THEN Statement. The three values entered are compared to get which is the greatest or biggest.

Line 50, Test a codition that if the value of A is greater than the value of B, and also if that same Value of A is greater than the value of C then control goes to Line 90. It prints a result that A is the greatest of the three numbers. The next line with a goto statement after Line 90 ends the program.

But if the above condition on line 60 is false, control goes to the next line which is line 60. Line 60 acts as Line 50 by comparing the values, and if the condition is true, contol jumps to Line 100. The next line after 100 is implemented. This ends the program.

If the compiller compares line 50 and 60 and they are both false, Line 70 is implemented. It carries a goto statement which changes the sequential execution of the program. Line 110 is implemented.

With the logic from the program above, you can compare more numbers. Line numbers are optional when using QBASIC.

Do you like this post? Please link back to this article by copying one of the codes below.

URL Of Post:

HTML Link Code:

BB (forum) link code: