Showing posts with label BASIC Programming. Show all posts
Showing posts with label BASIC Programming. Show all posts
Monday, November 16, 2015
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.
Read more »
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.
BASIC Program to Find Even Numbers Using FOR TO NEXT statement
CLS
Sum = 0
FOR even = 2 T0 50 STEP 2
Sum = Sum + Even
NEXT even
PRINT "The even numbers are:"; Sum
END
Sum is initialised to 0. For variable name (even), Start from 2, step two numbers and stop at 50. This is how the even numbers will be selected. You can choose to print the even numbers to the screen instead of adding them together and assigning it to sum. The program would be
For even = 2 TO 50 STEP 2
Next even
Print "The even numbers are :"; Even
End.
Read more »
Sum = 0
FOR even = 2 T0 50 STEP 2
Sum = Sum + Even
NEXT even
PRINT "The even numbers are:"; Sum
END
Sum is initialised to 0. For variable name (even), Start from 2, step two numbers and stop at 50. This is how the even numbers will be selected. You can choose to print the even numbers to the screen instead of adding them together and assigning it to sum. The program would be
For even = 2 TO 50 STEP 2
Next even
Print "The even numbers are :"; Even
End.
BASIC Program For Sum of Numbers Using IF Then Statement
5 CLS
10 Sum = 0
20 I = 1
30 Input "Enter a Number"; N
40 Sum = Sum + N
50 I = I + 1
60 IF I > 10 Then 80
70 Goto 30
80 Print " The sum is:" Sum
90 End
The first line CLS clears the screen of the previous program that was executed. Sum is initialised to 0. Always advisable to initialise it to 0. 'I' is the counter. You can choose to initialise it to 0 or 1. The keyword 'INPUT' obtains a number from the user by displaying a message 'Enter a number'.
Next line, the counter is incremented, and the compiller moves to the next line and assigns 0 + Entered number to Sum. The 'IF' statement sets a condition and if the condition is true, the program continues to line 80, if the condition is false, that is the counter is below ten, it goes back to line 30 requesting another number from the user.
The condition must be satisfied before the program prints the result to the screen.
Read more »
10 Sum = 0
20 I = 1
30 Input "Enter a Number"; N
40 Sum = Sum + N
50 I = I + 1
60 IF I > 10 Then 80
70 Goto 30
80 Print " The sum is:" Sum
90 End
The first line CLS clears the screen of the previous program that was executed. Sum is initialised to 0. Always advisable to initialise it to 0. 'I' is the counter. You can choose to initialise it to 0 or 1. The keyword 'INPUT' obtains a number from the user by displaying a message 'Enter a number'.
Next line, the counter is incremented, and the compiller moves to the next line and assigns 0 + Entered number to Sum. The 'IF' statement sets a condition and if the condition is true, the program continues to line 80, if the condition is false, that is the counter is below ten, it goes back to line 30 requesting another number from the user.
The condition must be satisfied before the program prints the result to the screen.
Monday, November 2, 2015
BASIC Program to Create Multiplication Table
This program displays the times table from 1 to 5 in rows and columns.
10 CLS
20 FOR I= 1 TO 5 STEP 1
30 Print using "###"; i;
40 Next i
50 For j= 2 to 10 step 2
60 Print using "###"; j;
70 Next j
80 For k = 3 to 15 step 3
90 Print using "###"; k;
100 Next k
110 For m = 4 to 20 step 4
120 Print using "###"; m;
130 Next m
140 End
You can follow the logic above to create the multiplication table of any size.
Subscribe to:
Posts (Atom)