MAT-Lecture-7 - Algorithms & Flowcharts
MAT-Lecture-7 - Algorithms & Flowcharts
MATHEMATICS
Lecture-8
•Implementation phase
implement the program in some programming language e.g.
Java, C, C++, C#, , VB.NET, etc
What is a computer program?
A computer program is a sequence of instructions written using a
Computer Programming Language to perform a specified task by the
computer.
Source Program Object
Object Program
Program
Written in High Level Compiler Machine
Machine Language
Language
Language
Codes/Instructions
Codes/Instructions
•A compiler is a software that converts the source code to the object code. In other
words, we can say that it converts the high-level language to machine/binary
language. Moreover, it is necessary to perform this step to make the program
executable. This is because the computer understands only binary language.
Variables and Constants
What is a variable?
Ideally, computer programs get input data from users, other programs
process the data and return output.
• Every piece of data (input, intermediate results, final output) the
computer works with must be stored inside the computer
• A variable is a storage location in computer memory reserved for a
specific data item
– Each variable must have a name or label, called variable-name or data-
name
– It is advisable to use variable-names that indicate the kind of data item
they represent, e.g. customerName could be a variable-name to store a
customer’s name
– Variable-names must not have space in between. Suppose you want to
create a variable with the words ‘customer name’, you can combine them
and then capitalise each first letter after the first word (i.e.
customerName) or you can use an underscore (_) to combine them (i.e.
customer_name).
What is a constant?
A constant is a data item that does not change during the execution of a
program
• Constants are sometimes called literals.
• You can have both numeric constants and nonnumeric constants
• Usually, constants are declared using uppercase letters, e.g. RATE,
while variables use lowercase letters, e.g. interest rate or interest Rate
Assigning values
Storing a value in a variable or constant is called assigning a value to the
variable or constant.
• To store a value in a variable or constant, we use the assignment
operator (=).
• The value to be assigned must always be to the right, while the variable
in which the value is to be stored must always be to the left.
• The following examples illustrate assignment: firstName=“John”
RATE=0.75 Balance=10000, X=100, Y=200
What is a algorithm?
•is a step-by-step list of instructions for solving a particular problem
•It is a series of steps for achieving a specific task
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 6: Stop
Write an algorithm to find the largest among three different
numbers entered by user.
Step 1: Start
Step 5: Stop
Write an algorithm to find a number is even or odd:
Step 1- Start
Step 6- Stop
Factorial of a number:
n ! = n × (n – 1) × (n – 2) ×………..3 × 2 × 1
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1= 6
4! = 4 * 3 * 2 * 1= 24
5! = 5 * 4 * 3 * 2 * 1= 120
Step 1: Input N
Step 2: Rev = 0
Step 3: While (N != 0)
Rem = N % 10;
Rev = Rev * 10 + Rem;
N = N / 10;
Step 4: Print Rev
Excercises:
4. A decision symbol must have two exit points, one for YES (TRUE)
and the other one for NO (FALSE)
Examples of flow charts:
1) Design a flowchart for a program that adds 2 numbers
2) Design a flowchart to find largest of 2 numbers
3) Design a flowchart to find largest of 3 numbers
4) Design a flowchart to find sum and average of three numbers
Loops
1 while (condition)
2 # statement 1
3 # statement 2
4 # ...
5 # statement n
6 end
Do-While loop(bottom testing)
1{
2 # statement 1
3 # statement 2
4 # ...
5 # statement n
6 }
7 while (condition)
8 end
1) Design a flow chart for displaying numbers from 1 to 10
2) Design a flow chart for summing of a N digit number:
Write a flowchart for a program that performs the following
tasks:
Allows the user to enter the name and mark for 10 students
at the keyboard and computes the average of the marks.
The program must also print all the name of students who
got marks below 50
Thank you!