QBASIC (Chapter 1) : Starting Programming

For any type of consultation, query or doubt. You may contact to the following: (+91) 9804 436 193 debabrataguha20@gmail.com 
and join the group  https://www.facebook.com/groups/331231161093613/ 




Hope you have installed QBASIC already and you are able to execute your QBASIC codes now.
  • After completing my course you would be able to complete your academic syllabus. But while learning, do not think about the academic syllabus. You just need to trust me. This is made for the people having zero experience in coding world.
  • It would be quick, if you read all the lines without thinking anything else or about any book suggested by your board or university. After 1 month ( yes!! just 1 month) you would be able to read your board/university suggested books & you would be able to understand everything and can do programming by your own
  • I have written few lines in the chapters instead of putting all the theories in one shot. So after reading the chapter quickly, start solving the exercise. You have to solve the exercise in QBASIC compiler. If you need any help for the installation of QBASIC in your smart phone or in your computer, just send me a mail to my mail id. Remember, it would be better to use the version 3 or later versions. If you do not know anything about QBASIC version, then simply jump into program. No need to be worried on QBASIC versions.

Solve all the programs. Once solved, send all the programs to my mail id , I would check and revert you back with all the necessary corrections. But you need to give an honest effort to solve the exercise.


You have only 5 things to learn in this chapters. After that, you will be able to solve all the problems where we do not need FOR loop or IF statement.



1.1 First Thing to learn:

Let’s start with some problems in the real life world. Like if I ask you what is your height and suppose you give me the answer like 6 feet. Now If I ask you that I need the length in cm, because I have to fill an army examination form. it is mandatory there to give your height in cm . So how can you give me the length in cm?

Obvious answer is to search it in google. There are multiple small apps which can convert a length in foot to centimeter equivalent. And the answer would be 182.88 cm. You just need to write “foot to cm converter” in google search engine.


Now write 6 in 1st box and in another box the centimeter equivalent will automatically come.


 So my question is now , how does google transform the length from foot to cm. Or what about those small apps which can convert temperature from Centigrade to Fahrenheit and lots more. So We will start learning how simply we can write this logic by our own.


Now If I want to write the simplest code by adding 2 numbers and print the sum, I will write the following code in QBASIC:

            a = 10
            b = 5
            c = a + b
print  c
end

Here we are working with 3 variables or boxes. 1 variable or box can hold any value inside. For example, box “a” is holding 10 , variable “b” is holding 5 and initially the variable “c” is not having any value. We would calculate the value of “c” at the runtime.
while running the program, QBASIC is putting the sum ( means 15 ) into the variable “c”.


Now, print  line basically prints something in the screen when you execute/run the program.

If we write , print “hi” it would print “hi” in the screen.
If we write , print “Debabrata” it would print “Debabrata” in the screen.
If we write , print “Debabrata Guha” it would print “Debabrata Guha” in the screen.
If we write , print “c” it would print “c”
If we write , print c without quotation , it would print the value of c, that is “15” in above program.
If we write , print “hi” ; c it would print “hi15”
So If we write , print “answer is : “; c it would print “answer is : 15”


The last part of the program was “END”. There is no significance of the keyword “END” . It just denotes the end of a program.




Now moving into our 2nd program,

If I ask you to take 2 numbers and show the addition and subtraction of them. Remember one thing QBASIC is not case sensitive , so it is not the matter to QBASIC if we write the program in Upper case or Lower case letters. I like to write the QBASIC program in Upper Case or in Capital letters. You may choose writing in lower case as well.


You can write the following program:

A = 10
B = 5
C = A + B
PRINT 
D = A - B
PRINT 
END

You can also write it in an alternative way like below:

A = 10
B = 5
C = A + B
PRINT 
C = A - B
PRINT 
END


Here in the alternative way we are calculating the sum and storing it into “C” and printing it , again we are calculating the difference and storing it inside “C” and printing it back.


Now, suppose we have a temperature in centigrade and we want the equivalent temperature in Fahrenheit. In this case we have to use the old physics formula as below:

c/5=(f-32)/9
or, 9c/5=f-32
or, f=(9c/5)+32



C = 16
F = ( 9 * C / 5 ) + 32
PRINT C ; “ CENTIGRADE “ ; F ; “ FAHRENHEIT “ 
END

So the output would be “16 CENTIGRADE= 60.8 FAHRENHEIT


And please be noted, in computer for multiplication you have to use “*” and for division you have to use “/”.







1.2   Second Thing to learn:

Now if we wish to make it more real time like the google app we saw earlier “Foot to cm converter”. We need to allow the user to enter the temperature in centigrade as per their choice and our program will return the temperature in Fahrenheit. Like in google we entered 6 by our choice and in another box it showed 182.88 cm.

So our programming would be

INPUT “ ENTER TEMPERATURE IN CENTIGRADE “ ; C
F = ( 9 * C / 5 ) + 32
PRINT C ; ” CENTIGRADE = “ ; F ; ” FAHRENHEIT ”  
END


Here you have found 1 new line as underlined above. The underlined line is being used to prompt a message to user saying “Enter the temperature in centigrade” , or can prompt “Please give a temperature value” etc etc. and then take an input from keyboard and convert it into Integer . Atlast store it inside the variable C.

Suppose we will take 2 numbers as input from users and will print the sum and product of them. Then the program will be like following:


INPUT “ENTER 1ST NUMBER “ ; A
INPUT “ENTER 2ND  NUMBER “ ; B

C=A+B
PRINT “ THE SUM IS “ ;  C  

C=A*B
PRINT “ THE PRODUCT IS “ ; C

END


Here we have written INPUT 2 times to take 2 inputs from keyboard by user.







1.3   Third Thing to learn:

Now we can create any small app and give it to our younger brother or sister. We can create following apps by the programming logic we learnt.
  • Centigrade to Fahrenheit conversion app.
  • Fahrenheit to Centigrade conversion app.
  • Meter to KM, CM conversion and vice versa. We had a chapter in mathematics in our childhood days.
  • Mensuration mathematics like entering 2 sides of rectangle and print area and perimeter.
  • Area , perimeter calculation for a triangle, circle and square.
  • In algebra, you will be given the value of a & b and you need to calculate (a+b)2

These apps will be useful in mathematics or physics homework. They can quickly complete their homework without doing any calculation and complete their homework in rapid speed like in 5-10 mins instead of spending the full evening. They just need to execute the program written by you and input the values. For giving this idea, I think mathematics teacher will not be happy at all.



So now for the app, where the user will give radius of a circle and will expect the area and perimeter of the same. Area and perimeter of circle almost always comes in decimal format, as we are multiplying the radius with PI (that is 3.14). The default QBASIC code generates all the numbers as Floating point values.


INPUT “ENTER THE RADIUS “ ; A
B = 2 * 3.14 * A
C = 3.14 * R * R

PRINT “ THE PERIMETER IS “ ; B 
PRINT “ THE AREA IS “ ; C

END


For your information,

Some examples of Integer numbers are 0, 1 , 2 , -3 , -9 , 999 etc 
Some examples of floating point numbers are 0.35 , 1.27  , 2.333333  , -3.12 , -9.0 , 999.9999999 etc.


Now take another example:

A = 9
B = 2
C = A / B
PRINT 
END


Above program will print the result as 4.5 by default.


But if we use “int” method at the time of storing the result into c , it would print the answer as 4 . Integer variable never shows the decimal part, it discards the point values.

A = 9
B = 2
C = INT ( A / B )
PRINT  C
END







1.4   Fourth Thing to learn:


Suppose , it is needed to do determine x5 or y4 in any program. The way you will follow now is to write x*x*x*x*x and y*y*y*y
  • If I need to determine x34 , will you write x 34 times?
  • If I need to determine xy and x ,y are unknown and will be given by user in run time. In this case you would not be able to write the code even.

So there is a short-cut to this. Simply write,

Z = X ^ Y

or directly you can write

Z = 5 ^ 3


So now assume there is program where user needs to enter 2 number as x & y, in return he/she will get

INPUT “ ENTER 1st NUMBER “ ; X
INPUT “ ENTER 2nd NUMBER “ ; Y

Z =( X ^ 2   +   Y ^ 2 ) ^ 0.5

PRINT   “ THE RESULT IS “ ; Z 

END



1.5   FIFth Thing to learn:  

To calculate the remainder after mathematical division in QBASIC, we use ‘%’. Suppose user will give 2 number as input you need to calculate result, remainder after division, we need to write the program in following way:


INPUT “ ENTER 1ST NUMBER “ ; A
INPUT “ ENTER 2ND NUMBER “ ; B

C = INT ( A / B ) 
D = A MOD B

PRINT  “ THE RESULT IS “ ; C  
PRINT “ THE REMAINDER IS “ ; D 

END


So if user enters 2 numbers as 38 & 5. The output will be as follows:

THE RESULT IS 7 
                        THE REMAINDER IS 3 
Remember,
                              Here result came as 7 because INT() always returns an integer , so it can not return 7.6




So we are done with the 5 things, now we can build all the small apps where we do not need the help of IF statement or FOR loop. We will learn IF & FOR in immediately next chapters.

So now we can start solving the exercise. So before starting the exercise, we need to maintain 1 small best practice like below:

Try to give a meaningful names to variables. Instead of giving a,b,c,d  , try to give names like num1 , num2, div, rem respectively. Later when you will be dealing with a hundred line program, it will be tough to handle and remember those variables with names a,b,c etc. Better to call them by their meaningful names.

That’s all for this chapter. Now we can jump into the exercise. You can find all the exercise in the facebook group album named “Basic Exercise”



Please leave your comments here in the comment box. Give your feedbacks with the good parts of this material or suggest where you are still facing difficulties.







Comments

Popular posts from this blog

Java concepts Theory

About Myself ..