Lab 9
Lab 9
Lab – 9
Date:9/dec/2024
1|Page
Fall 2024 QCR1 Lab 9 Version 1.1
Document History
2|Page
Fall 2024 QCR1 Lab 9 Version 1.1
Lab Outcomes
Students will gain a clear understanding of the algorithm design and their working.
CLO2: Reproduce an algorithm for a given problem, and can implement its solution through C++ programming language.
CLO3: Present concise yet comprehensive technical reports.
Equipment
Computer
o Flowgorithm
Instructions
The following instructions are to be followed while performing in the labs.
The manual must be thoroughly read before starting the lab.
The theoretical concepts related to the lab and experiments must be revised.
All attempts shall be made to complete the lab during the lab session.
Any attempt to plagiarize from any source will be reported to the disciplinary
committee forfurther action so keep the work original.
Carefully use the laboratory equipment.
3|Page
fa
Introduction to Algorithms:
An algorithm is a step-by-step procedure or set of rules designed to solve a specific problem or
perform a particular task. It can be thought of as a recipe for solving a problem in a finite number
of steps. Each step is precisely defined, and the order in which the steps are executed is important.
Algorithm have a specific input, a process that takes those input, conduct some type of processing
and produces output.
Student will first write an algorithm and then implement it on software to see its working and output
graphically and visually.
Example 1: Write an algorithm to add two numbers and produce its output.
1. Start
2. Initialize a as 10
3. Initialize b as 5
4. Initialize sum as 0
5. sum=a+b
6. Output sum
7. End
Flowchart
Output of Flowchart
4|Page
fa
Control Statements
Normally, statements in a program execute one after the other in the order in which they’re written. This is
called sequential execution. Various C++ statements we’ll soon dis cuss enable you to specify that the next
statement to execute may be other than the next one in sequence. This is called transfer of control.
Example 2: Design an algorithm for student grading system. Ask the user to input variable “number”. If
number value is greater than or equal to 90, output grade as A.
If number value is greater than or equal to 80, output grade as B.
If number value is greater than or equal to 70, output grade as C.
If number value is greater than or equal to 60, output grade as D.
If number value is less than 60, output grade as F.
Algorithm:
Start
Declare a variable number.
Ask user to input variable
number.
If(number > =90)
Output”Grade is A”
ElseIf(number > =80)
Output”Grade is B”
ElseIf(number > =70)
Output”Grade is C”
ElseIf(number > =60)
Output”Grade is D”
Else
Output”Grade is F”
Stop
5|Page
fa
Flowchart
Output
6|Page
fa
Repetition Statements:
A repetition statement specifies that a program should repeat an action while some condition remains true. This process
refers to loop execution as well.
Loops can be implemented through three different concepts
a) while loop
b) for loop
c) do while loop
Example Write Algorithm to find the sum of first ‘n’ natural numbers.
Start
Input n
sum = 0
i = 1
while (i <= n)
sum = sum + i
i = i + 1
end while
Print sum
End
Flow Chart
Output
7|Page
fa
Lab Tasks
Make an algorithm and flow chart along with its output for following specifications:
a) Algorithms take value of three variables from user. Using If / If-elseif-else design an algorithm
to compute largest variable among them.
Algorithm:
Start
Input a, b, c
If (a>b and a>c) then
“Largest a”
Else if(b>a and b>c) then
“Largest b”
Else
“largest c”
End if
End
Flow chart:
8|Page
fa
Outputs A:
Output B:
Output C:
9|Page
fa
b) Algorithm to take value of three variables from user. Output the product of these variables.
Start
Input a,b,c
Product = a*b*c
“Display product”
End
Flowchart:
Outputs:
10 | P a g e
fa
c) Using loop concept, make an algorithm for computing factorial of a number “n”.
Start
Input n
Factorial = 1
For i = 1 to n
Factorial = factorial * i
i = i+1
Output factorial
End
Flowchart:
11 | P a g e
fa
12 | P a g e