Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

BASIC Program For Sum of Numbers Using IF Then Statement

If you're wondering how to write a basic Program, then here is a simple program in the BASIC language for the Sum of Numbers Using the IF Then Statement which will help you begin.

One of the ways to learn to programme is to try out some simple BASIC program examples in order to wrap your head around the concept of programming.

BASIC as one of the simplest programming languages for beginners is easy to understand, code, and debug and for this reason, it is usually used as a foundation course.

Looking at the program code below, you'll definitely agree with me since one can easily tell what each line actually does even without any programming skills.

In this qBASIC program, we are going to request for 9 random numbers from the user which will be used in the computation and then we will employ some BASIC statements in programming (Looping and Decision statements).


Also Read: BASIC Program to Create Multiplication Table
                   Pascal Program of Matrix Multiplication

Program Code


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

How The Program Code Works

The first line CLS, clears the screen of the previous program that was executed, that is if there was any. This is an optional step but then, you can't be too sure that there is currently no value being held in memory. I can say it's just as useful.

In line 10, Sum as our first variable is initialized to 0 (It is always advisable to initialize it to 0 in order not to influence the final result). The result of each computation will be assigned to this variable whose value is certainly going to vary each time.

In line 20, we have 'I' as the program counter that will ensure that we do not have more than 9 numbers. On the other hand, if 'I' is initialized to 0, we will most certainly have 10 numbers. That being the case, the choice of initializing this variable to either 1 or 0 is left for you.

The keyword 'INPUT' will be used to obtain a number from the user and it does this by displaying a message 'Enter a number'. A user will know what is expected of them just by seeing this prompt message.

Next line, the value entered by the user is added to SUM whose value changes to the current number. So, if the first value entered by the user is 5, then the value in memory will switch from 0 to 5.

In line 50, our program counter 'I' is incremented to 2 to build up the loop so that it gets to 10. Also, the compiler will be expecting a second number 

The 'IF' statement on line 60 may be the most important part of this program since this decision statement will decide if we have gotten exactly 9 numbers.

Here, an IF condition is set to only proceed with printing the result of the program on line 80 if our program counter has incremented to 10.

On the other hand, if this is not the case, the Goto statement returns control to line 30 of the program. Instead of the Goto statement, we could've used an Else statement on that line.

Nevertheless, the condition must be satisfied before the program prints the result to the screen and then moves to our end statement.

It's as easy as that!

Conclusion:

That's a simple BASIC Program For the Sum of Numbers Using the IF Then Statement. Did you encounter problems? Let us know and we'll guide you through it. For more examples of basic programming language, you can check out the posts below.


Also Read: BASIC Program to Find Even Numbers Using FOR TO NEXT statement
                   BASIC Program to Compare The Greatest of Three Numbers




This post first appeared on Techlass, please read the originial post: here

Share the post

BASIC Program For Sum of Numbers Using IF Then Statement

×

Subscribe to Techlass

Get updates delivered right to your inbox!

Thank you for your subscription

×