LECTURE-3 Algorithm & Flowchart
LECTURE-3 Algorithm & Flowchart
-271
Course Title: Introduction to Computer Programming
Presented By
Saadman Sakib
Lecturer,
Department of CSE,
CUET
Algorithm
Flowchart
program.
Read Pay Rate
Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Display Gross
Pay
END
Rounded
Basic Flowchart Symbols START Rectangle
Display message
“How many
hours did you
work?”
Multiply Hours
by Pay Rate.
START Store result in
Gross Pay.
Display Gross
Pay
END Terminal
END
Basic Flowchart Symbols START
Display message
“How many
hours did you
work?”
– represented by parallelograms
Display message
– indicate an input or output “How much do Input/Output
you get paid per
operation hour?” Operation
Multiply Hours
by Pay Rate.
Display message Store result in
Gross Pay.
“How many
Read Hours
hours did you Display Gross
Pay
work?”
END
START
Basic Flowchart Symbols
Display message
“How many
hours did you
work?”
– represented by rectangles
Display message
– indicates a process such as a “How much do
you get paid per
mathematical computation or hour?”
variable assignment
Read Pay Rate
Multiply Hours
by Pay Rate.
Process Store result in
Multiply Hours Gross Pay.
by Pay Rate.
Store result in Display Gross
Pay
Gross Pay.
END
Stepping Through
Stepping Through the
START
Output
Operation
the Flowchart
Display message
“How many
Flowchart hours did you
work?”
Read Hours
How many
hours did
you work?
Display message
“How much do
you get paid per
hour?”
Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.
How many
Input Read Hours
hours did Operation
you work?
(User types Display message
40
40) “How much do
you get paid per
hour?”
Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.
Hours: 40
Display Gross
Pay Rate: ? Pay
the Flowchart
Display message
“How many
Flowchart hours did you
work?”
Read Hours
How much
do you get
paid per
Display message
hour?
“How much do
Output you get paid per
Operation hour?”
Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.
Hours: 40
Display Gross
Pay Rate: ? Pay
the Flowchart
Display message
“How many
Flowchart hours did you
work?”
Read Hours
How much
do you get
paid per
Display message
hour? 20
“How much do
you get paid per
hour?”
Hours: 40
Display Gross
Pay Rate: 20 Pay
Read Hours
How much
do you ge
paid per
Display message
hour?
“How much do
you get paid per
hour?”
Multiply Hours
Process: The by Pay Rate.
Store result in
Variable Contents: product of 40
times 20 is
Gross Pay.
Hours: 40 stored in
Gross Pay Display Gross
Pay Rate: 20 Pay
Read Hours
Your gross
pay is 800
Display message
“How much do
you get paid per
hour?”
Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.
Hours: 40
Output Display Gross
Pay Rate: 20 Operation Pay
• Sequence
• Decision
• Repetition
• Case
Sequence Structure
NO YES
Decision Structure
Flowchart
C++ Code
NO YES
if (x < y)
x < y?
a = x * 2;
else
Calculate a Calculate a
as x plus y. as x times 2. a = x + y;
Decision Structure
Calculate a
as x times 2.
Repetition Structure
while (x < y)
YES x++;
x < y? Add 1 to x
Controlling a Repetition Structure
YES
x < y? Display x Add 1 to x
Case Structure
If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400
If years_employed = 1, If years_employed is
CASE
bonus is set to 100 years_employed any other value, bonus
is set to 800
1 2 3 Other
Flowchart START
Read A, B, C
No No
END
The biggest of three inputted numbers
Algorithm
Step 1: Input A, B, C
Step 2: if (A>B) then
if (A>C) then
MAX A [A>B,A>C]
else
MAX C [C>A>B]
endif
else
if (B>C) then
MAX B [B>A, B>C]
else
MAX C [C>B>A]
endif
endif
Step 3: Print “The largest number is”, MAX
The biggest of three inputted numbers
#include <stdio.h>
int main( )
{
float A,B,C,MAX;
SOURCE CODE printf("Enter the values of a,b and c:\n");
scanf("%f%f%f",&A,&B,&C);
if(A>B)
{
if(A>C)
MAX=A;
else
MAX=C; }
else
{
if(B>C)
MAX=B;
else
MAX=C; }
printf("Biggest Number=%f\n",MAX);
return 0; }
Leap Year
Start
procedure leap_year()
Step 1 → Take integer variable year
Step 2 → Assign value to the variable IF year%4 = 0 AND
Step 3 → Check if year is divisible by 4 but year%100 != 0 OR
not 100, DISPLAY "leap year“ year%400 = 0
Step 4 → Check if year is divisible by 400, PRINT year is leap
DISPLAY "leap year" ELSE
Step 5 → Otherwise, DISPLAY "not leap PRINT year is not leap
year" STOP END IF
End
end procedure
Algorithm Pseudocode
https://www.tutorialspoint.com/learn_c_by_examples/leap_year_program_in_c.htm
Flowchart
https://www.tutorialspoint.com/learn_c_by_examples/leap_year_program_in_c.htm
Source Code
#include <stdio.h>
int main() {
int year;
while(scanf(“%d”,&year)!=EOF)
{
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
}
return 0;
}
Advantages and Disadvantages of Flow Chart
Advantages
1. Communication: Flow Charts are better way of communicating the logic of a system to all
concerned.
2. Effective Analysis: With the help of flow chart, problem can be analyzed in more effective way.
3. Proper Documentation: Program flow harts serve as a good program documentation, which is
needed for various purposes.
4. Efficient Coding: The flow charts act as a guide or blueprint during the systems analysis and
program development phase.
5. Proper Debugging: The flow chart helps in debugging process.
6. Efficient Program Maintenance: The maintenance of operating program becomes easy
• with the help of flow chart. It helps the programmer to put efforts more efficiently on that part.
Disadvantages
1. Complex logic: Sometimes, the program logic is quite complicated. In that case, flow chart
becomes complex and clumsy.
2. Alterations and Modifications: If alterations are required the flow chart may require re-
drawing completely.
3. Reproduction: As the flow chart symbols cannot be typed, reproduction of flow chart
becomes a problem.