Chapter 1 :Algorithm and Flowchart



#Algorithm

Algorithm is a sequence of step by step instructions.

#Algorithm to make a cup of tea.

Step 1: Start
Step 2:fill kettle with water.
Step 3: boil the water in kettle.
Step 4:add milk, tea leaves and sugar.
Step 5: stir the tea.
Step 6 :wait until it boils
Step 7:Stop/End


Assignment

1. Write an algorithm to eat your dinner.

2. Write an algorithm to buy a pen.

3. Write an algorithm to turn on the computer.

4. Write an algorithm to download an application(app) on mobile phone.

5. Write an algorithm to stay for online classes.


1. Write an algorithm to add 10 and 20

Step 1: Start

Step 2: a=10, b=20

Step 3: sum= a+b

Step 4: Print sum

Step 5: End


2. Write an algorithm to add two numbers

Step 1: Start

Step 2: Input a,b

Step 3: sum= a+b

Step 4: Print sum

Step 5: End


3. Write an algorithm to subtract three numbers.

Step 1: Start

Step 2: Input a,b and c

Step 3: difference= a-b-c

Step 4: Print difference

Step 5: End


4. Write an algorithm to multiply two numbers.

Step 1: Start

Step 2: Input a and b

Step 3: product= a*b

Step 4: Print product

Step 5: End


5.Write an algorithm to divide two numbers.

Step 1: Start

Step 2: Input a and b

Step 3: divide= a/b

Step 4: Print divide

Step 5: End


6.Write an algorithm to find area of square (hint: area of square=L*L)

Step 1: Start

Step 2: Input L

Step 3:area of square= L*L

Step 4: Print area of square

Step 5: End


7.Write an algorithm to find area of rectangle (hint: area of rectangle=L*b)

Step 1: Start

Step 2: Input L and b

Step 3:area of rectangle= L*b

Step 4: Print area of rectangle

Step 5: End


8. Write an algorithm to find area of circle.(hint: area of circle=3.14*r*r)

Step 1: Start

Step 2: Input r

Step 3:area of circle=3.14*r*r

Step 4: Print area of circle

Step 5: End


9.Write an algorithm to find perimeter of square.(hint: perimeter of square=4*L)

Step 1: Start

Step 2: Input L

Step 3:perimeter of square=4*L

Step 4: Print perimeter of square

Step 5: End

10. Write an algorithm to find perimeter of rectangle.(hint: area of square=2*(L+b)

Step 1: Start

Step 2: Input L abd b

Step 3:perimeter of rectangle=2*(L+b)

Step 4: Print perimeter of rectangle

Step 5: End


11. Write an algorithm to find out greatest of any three given number.


Step 1: start

Step 2:Input a, b and c

Step 3: if a>b and a>c then

Step 4: Print a is greatest else b>c and b>a

Step 5: Print b is greatest else Print c is greatest

Step 6: Stop


12. Write an algorithm to find out smallest of any three given number.
Step 1: start
Step 2:Input a, b and c
Step 3: if a<b and a<c then
Step 4: Print a is smallest else b<c and b<a
Step 5: Print b is smallest else Print c is smallest
Step 6: Stop

13. Write an algorithm to find whether the given number is odd or even.

Step 1: start
Step 2:Input a
Step 3: if a/2 and remainder is 0
Step 4: Print a is even else Print b is odd
Step 5: Stop

14. Write an algorithm to read principle, interest, time and find simple interest(SI).
[Note: SI=(P*T*R)/100]

Step 1: start
Step 2:Input P, T and R
Step 3: SI=(P*T*R)/100
Step 4: Print SI
Step 5: Stop

15. Write an algorithm to convert centigrade into Fahrenheit.

Step 1: start
Step 2:Input C
Step 3: F=(C*9/5)+32 Step 4: Print F
Step 5: Stop

16. Write an algorithm to find volume of cuboid.

Step 1: start
Step 2:Input L,b and h
Step 3:volume=L*b*h
Step 4: Print volume
Step 5: Stop

17.Write an algorithm to convert meter into centimeter.

Step 1: start
Step 2:Input m
Step 3:c= m*100
Step 4: Print c
Step 5: Stop

18.Write an algorithm to read three numbers and find its average.


Step 1: start
Step 2:Input x, y and z
Step 3:average=(x+y+z)/3
Step 4: Print average
Step 5: Stop

Flowchart

  • A flowchart can also be defined as a diagrammatic representation of an algorithm.

  •  The flowchart shows the steps as boxes of various kinds, and their order by connecting the boxes with arrows.

Flowchart Symbols






1. Draw a flowchart to add 10 and 20.


2. Draw a flowchart to take input from user and add those number.

3. Draw a flowchart to find area of rectangle.

4. Draw a flowchart to take input from user and subtract those number.

5. Draw a flowchart to find area of square.

6. Draw a flowchart to find area of circle.

7. Draw a flowchart to find perimeter of rectangle.

8. Draw a flowchart to find perimeter of square.

9. Draw a flowchart to find volume of cube.

10.Draw a flowchart to find volume of cuboid.

11.Draw a flowchart to take input from user and find whether the number is odd or even.

12.Draw a flowchart to take input from user and find greatest among two numbers.
13.Draw a flowchart to take input from user and find greatest among three numbers.


Answer the following questions.


  1. What is a program?

A set of instructions that tells the computer how to solve a problem or perform a task is known as a program.


  1. What is an algorithm? List out any three properties of an algorithm.


algorithm has the following properties:

  • Each step of the algorithm should be simple. 

  • The logic of each step should be clear.

  • It should be effective.


  • An algorithm must come to an end after a finite number of steps.

  •  Every step of the algorithm must be accurate. At the same time, it must have a time limit for execution.

  • The output must be logically correct.



  1. What are the advantages of an algorithm?

  • They are simple to understand and provide a step-by-step solution for a problem. 

  • Errors can be pointed out very easily. Debugging is simple.

  •  They do not depend on any of the programming languages.

  • They are compatible with computer languages.


  1.  What are the disadvantages of an algorithm? 

Disadvantages

  • Algorithms are time-consuming.

  • Big tasks are difficult to put in algorithms.

  • Difficult to show branching and looping in algorithms.

  • Understanding complex logic through algorithms can be very difficult.


  1. What do you understand by the term flowchart?


  1. State any three advantages and disadvantages of a flowchart.

advantages

  • It helps in reviewing and debugging a program.

  • It helps to detect deficiencies in the problem statement.

  • It helps the programmer in developing the program logic and to serve as documentation for future reference for a completed program.

  •  It helps in the discussion of a solution with others or in explaining the program to others.


Disadvantages

  • Drawing a flowchart is time consuming. 

  • Making changes in the flowchart is not an easy job. Re-drawing of flowchart is required.


  • If the logic of the problem is complex, the flowchart becomes complicated. 

  • Flowchart cannot be directly converted into program code.


  1. What are the various basic symbols used in flowcharting? Give their pictorial representation.


  1. What is a pseudocode? What are the advantages and disadvantages of pseudocode?

An informal language used to express the flow of a program is known as pseudo code.


The advantages of pseudocode are listed below:

  • Writing of pseudocode involves less time and effort than drawing an equivalent flowchart

  • It can be easily modified.

  • Converting a pseudocode to a programming language is much easier than converting a flowchart to a programming language.


The disadvantages of pseudocode are listed below 

  • It becomes difficult to understand the complex logic written in pseudocode.

  • When too many nested conditions are used in the pseudocode, the level of difficulty to understand the code increases.

  • In the case of pseudocode, a pictorial representation of program logic is not available.


Fill in the blanks


  1. The instructions to the computer are given in the form of a ………..….. to carry out a specific task.

  2. ………..….. is a set of step by step instructions given in a logical order to solve a problem.

  3. ………..….. is a graphical representation of an algorithm.

  4. ………..….. box is used to show the steps involving comparison and decision making steps.

  5. ………..….. is an artificial and informal language that helps programmers develop algorithms.


State whether the following statements are True or False.


  1. Algorithm consists of a standard set of symbols of different shapes which are connected by arrows.

  2.  The flowchart helps in the discussion of a solution with others or in explaining the program to others. 

  3.  Decision box indicates any calculation or manipulation. 

  4.  Connectors are used to connect the flowchart symbols.


Chapter 2 :Qbasic

Introduction to QBASIC

BASIC stands for 'Beginners All Purpose Symbolic Instruction code'. It was founded in 1963 at Dartmouth college, by the mathematicians John George Kemeny and Tom Kurtzas.

BASIC is an Interpreter.

Q stands for 'Quick'

QBASIC- Quick Beginners All Purpose Symbolic Instruction code


Features of QBASIC

(a) It is easy to understand the language and write programs using it.

 (b) The words used in it are similar to the English words used in daily life like LET, INPUT, PRINT etc.

(c) Many tasks can be done easily from the menus available in it.

(d) If there are any mistakes while writing the program, it can be easily corrected. 

(e) It can be used to write a program, view its output and correct mistakes using more than one window.

(f) Help can be used as required.

Elements of Qbasic programming

  1. Character set

  2. Variables

  3. Constants

  4. Operator and operand

  5. Expression

  6. Statement


  1. Character set

Character set refers to the letters, numbers, and groups of different symbols that can be used in QBASIC. The Character set used in QBASIC is divided into the following three groups:

(a) Letters: A to Z (small and capital letters)

 (b) Numbers: 0 to 9

(c) Special symbols: =, +, *, -, /, ^, $, <>, (),%, #,!, &,? Etc.

  1. Variables

The names used to represent the various digits, letters, and values ​​used in the program, Variables represent a specific area of ​​memory, in which the stored values ​​can be changed as needed. It is of two types.

(a) Numeric variables

(b) String variables


(a)Numeric variables

The variables used to store numbers are called numeric variables. For example: x = 5, Age = 15, Salary = 15000 etc. Here x, Age / Salary can store numbers, which are called Numeric variables. Mathematical operations can be done on numeric variables like addition, subtraction, multiplication, division etc.


(b)String variables

Variables representing the letters or groups of characters used in the program are called string variables. String variables are used to store a person's name, address, etc.

For example: 

name$ = "Ram",

address$ = "Nepal",

Here name$ and address$ are called string variables. If the string variables have an '$' sign at the end, the value to be stored in it is written inside.




  1. Constants

The various numbers or letters used in a program that do not change when the program is run are called constants. There are two types of constants:

(a) Numeric constants 

(b) String constants


(a) Numeric constants 

Numeric constants positive) or negative numbers, in which mathematical operations can be added, subtracted, multiplied, divided, etc., are called numeric constants, such as 5,19,0,9.8 etc. are numeric constants. The comma (,) and blank spaces cannot be placed in the numeric constant.


(b) String constants

A group of different numbers or letters used in a program that are placed within a quotation mark ("") are called string constants, such as: "good morning", "01-5246610", "A215", etc. Blank spaces or any other symbols can be used in string constant. Mathematical operations like addition, subtraction, multiplication, division etc. cannot be performed on a string constant.


  1. Operators and operands 

In the programming language, various signs or symbols used to perform mathematical or other tasks are called operators. For example: '+' to add, '-' to subtract. Operators that work on values  That is called operand.


E.g.    5 *3


5 and 3 is operand 

* is operator


  1. Expression 

 Expressions are written to do certain things with the help of different operators and operands. The algebraic expressions we write in our daily lives cannot be understood in the same way as QBASIC. Therefore, the table to convert algebraic expression to QBASIC expression is presented in the following examples:



Algebraic Expressions

Qbasic Expression

2a+b

a÷b

ax2+bx+c

P=2(l+b)

2*a+b

a/b

a*x^2+b*x+c

P=2*(l+b)


  1. Statements

The instructions used in the program to perform certain tasks are called statements. When writing a program using any programming language, you have to write statements following certain rules of that language. The statements written in the QBASIC program are first stored in the computer's memory and only after running the program do they show certain results.


Statement of Qbasic are

Rem

CLS

Let

Input

Print


Rem

REM statement is a non-executable statement and stands for remarks. 


CLS

CLS statement is used to clear the output screen. 


Input

It helps to take input from user. we will take it only as an alphabet (a,b,c,d,..........)

e.g.- Input "Enter a number"; a

Input "Enter your name"; a$


Print

Using Print tag we can print anything we want in our output screen.

e.g.- Print "My school's name is Manaslu School"


END

END statement is used to end the program execution



1.   Write a Qbasic program to find sum of two numbers

Rem to find sum of two numbers

CLS

Let A=12

Let B=8

Let C=A+B

Print “Sum=”;C

END

 Output: Sum=20

2.   Write a Qbasic program to find area of rectangular room

Rem to find area of room

CLS

Let L=12

Let B=10

Let A=L*B

Print “Area of room=”;A

END

 Output: Area of room=120

 

3.   Write a Qbasic program to read the value of radius and find area of circle

Rem to find area of circle

CLS

Input “Enter radius of circle=”;R

Let Area=3.14*R*R

Print “Area of circle=”;Area

END

Output Enter radius of circle= 8

       Area of circle=200.96     

4.   Write a Qbasic program to read three numbers and find average of three numbers

Rem to find average of three numbers

CLS

Input “Enter first number=”;a

Input “Enter second number=”;b

Input “Enter third number=”;c

Let Average= (a+b+c)/3

Print “Average=”; Average

END

Output Enter first number= 10

Enter second number=8

Enter third number= 5

Average=7.67

 Rem to find average of two numbers

CLS

Input “Enter first number=”;a

Input “Enter second number=”;b

Let Average= (a+b)/2

Print “Average=”; Average

END

Output Enter first number= 10

Enter second number=8

Average=9


5.   Write a Qbasic program to read name, address and age and display it.

Rem to display name, address and age

CLS

Input “Enter your name:”;a$

Input “Enter your address:”;b$

Input “Enter your age:”;c

Print “Name-”;a$

Print “address:”;b$

Print “age:”;c

END

Output Enter your name: Ram

       Enter your address: Nayabazar

       Enter your age:15

Name-Ram

Address: Nayabazar

Age:15

END


Convert following algebraic expressions into Qbasic expression:


  1. 2xy

Ans:2*x*y

  1. a²+b²

Ans:a^2+b^2

  1. I= PTR/100

Ans:I=(P*T*R)/100

  1. y=mx+c

Ans:y=m*x+c

  1. A=r²

Ans:A=r^2


Write down the output of the following programs.

1.Rem to find product of two numbers

CLS

LET M=7

LET N=5

LET P=M*N

PRINT "Product of two numbers = ", P

END


Ans:Output: Product of two numbers =35


2.REM to increase value of a variable by 15%

CLS

LET A = 100

LET A=A+ A * 15/100

PRINT "New value of A="A

END

Ans: Output: New value of A=115


3.Remark to subtract two numbers

CLS

LET A = 20

Let B=10

C=A-B 

PRINT "Difference between two numbers=”; C

END

Ans:Output: Difference between two numbers=10


4.Rem to print name and age

CLS

Age = 13

Name$ = "Sundar"

PRINT "Name is : ", Name$

DISPLAY "Age is: ", Age

END

Ans:Output: Name is :Sundar

Age is: 13




Write a Qbasic program


(A)To show the name, address and total number of students in your class.

Rem to display name, address and total number of students in my class.

CLS

Input “Enter your name:”;a$

Input “Enter your address:”;b$

Input “Enter total number of students in your class.:”;c

Print “Name-”;a$

Print “address:”;b$

Print “total number of students in your class:”;c

END

Output:

Name-Urgen Bal

address: Nayabazar

total number of students in your class: 42

(B) To find simple interest (I=PTR / 100)

Rem to find simple interest 

CLS

Input “Enter principle:”;P

Input “Enter time:”;T

Input “Enter Rate”;R

Let I=(P*T*R)/100

Print “Simple interest=”;I

END

Output:

Enter principle:1000

Enter time: 2

Enter Rate: 10

Simple interest=200


 (c) To convert rupee into paisa

Rem to convert rupee into paisa

CLS

Input “Enter rupee:”;r

Let a=r*100

Print “paisa=”;a

END

output

Enter rupee: 12

paisa=1200










(D) To convert a given temperature in centigrade to Fahrenheit (F = 9C / 5 + 32)

Rem to convert a given temperature in centigrade to Fahrenheit

CLS

Input “centigrade: ”;c

Let F=c * (9 / 5) + 32

Print “Fahrenheit =”;F

END

OUTPUT:

centigrade: 32

Fahrenheit =89.6



(E) To find the average of any three numbers.


(F) To take a number and print it square (if 5 is given 25) 

Rem to take a number and print it square

CLS

Input “Enter a number: ”;a

Let X=a*a

Print “square =”;X

END

Output:

Enter a number: 7

square = 49

(G)To Find the perimeter of a circle.(P=2πr)


(H) To find the volume of a wax (V = LxBxH)


(I) To find out the value of an item and add 13% value added tax (VAT) to its total value.

REM to add VAT 13%

CLS

Input”Enter a number=”;a

LET C=a+ a* 13/100

PRINT "VAT="C

END


Output

Enter a number=100

VAT=113



Revision Questions


  1. What is a flowchart? Draw symbols used in flowchart and write its functions.

  2. Draw a flowchart to find sum of any five positive numbers

  3. Write an algorithm and draw a flowchart to find greatest among any four different numbers.

  4. Difference between algorithm and flowchart

  5. Write a Qbasic program to read value of base and height of a triangle and find its area

  6. Explain function of PRINT, LET

  7. What is a computer?

  8. Explain characteristics of computer?

  9. What is CPU?

  10. Explain function of computer briefly.

  11. What are demerits of computer?

  12. What is hardware?

  13. Define software with any four examples.

  14. Difference between input and output devices

  15. Explain function of PRINT, LET, INPUT

Comments