3.algorithms and Flowcharts Continued PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Dr. Balu L.

Parne
Assistant Professor,
CoED, SVNIT Surat.
Introduction to Flowchart
 A flowchart is a visual representation of the sequence of
steps for solving a problem .
 A flowchart is a set of symbols that indicate various
operations in the program.
 A flowchart gives a pictorial representation of an
algorithm.
 It is a symbolic diagram of operations sequence,
dataflow, control flow and processing logic in
information processing.
Why Flowchart is needed?
 A graphical representation of an algorithm, often
used in the design phase of programming to work
out the logical flow of a program.
 Visual way to represent the information flow
 Quick method of showing program flow
 Make our logic more clear
 Help during writing of program
 Make testing and debugging easy
Flowchart: basic symbols
Computation

Input / Output

Decision Box

Start / Stop
Flowchart: basic symbols…
Flow of
control

Connector
Flowchart or Program Constructs:
 Sequence: The order of execution, this typically refers to
the order in which the code will execute. Normally code
executes line by line, so line 1 then 2 then 3 and so on.

 Selection: Selection, like branching, is a method of


controlling the execution sequence, you can create large
control blocks, using if statements testing a condition, or
switch statements evaluating a variable etc. to control
and change the execution of the program depending on
this environment and changing variables.
Flowchart or Program Constructs:
 Iteration (Repetition): Iteration is typically used to refer to
collections and arrays of variables and data. Repeating set of
instruction. Counting from 1 to 10, you are iterating over the
first 10 numbers. for, while, do-while loops will be
implemented for iteration.
Flowchart Constructs:
Flowchart Constructs:
Flowchart Constructs:
Example :
 Example 1: Write an algorithm and draw a flowchart to find
the sum of two numbers.
 Example 2: Write an algorithm and draw a flowchart to find
the average of 3 numbers.
 Example 3: Write an algorithm and draw a flowchart to find
the largest among given two numbers.
 Example 4: Write an algorithm and draw a flowchart to
determine a student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as the average
of four marks. To pass the exam average of four subject must be
greater than or equal to 50.
Examples of Algorithm:
 Example 1 : Add two numbers.
 Step 1: Start
 Step 2: Read 2 numbers as A and B
 Step 3: Add numbers A and B and
store result in C
 Step 4 : Display C
 Step 5: Stop
Examples of Algorithm:
 Example 2 : Average of 3 numbers.
 Step 1: Start
 Step 2: Read the numbers a , b , c
 Step 3: Compute the sum and divide by 3
 Step 4: Store the result in variable d
 Step 5: Print value of d
 Step 6: End
Examples of Algorithm:
 Example 3 : Maximum of 2 numbers.
 Step 1 : Start
 Step 2 : Input A , B
 Step 3 : if A > B then output A
else output B
 Step 4 : Stop
Examples of Algorithm:
 Example 4 : Grade Marks.
Step 1: Start
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Step 4: Stop
 Example 4 : Grade
Marks.
Example :
 Example 5: Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate its area.
 Example 6: Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
 Example 7 : Write an algorithm and draw a flowchart to
converts the input Fahrenheit degree into its Celsius degree
equivalent. Use the formula: C= (5/9)*F-32.
 Example 8: Write an algorithm and draw a flowchart to Find
whether a given number is odd or even.
Examples of Algorithm:
 Example 5 : Area of rectangle.
 Step 1: Start
 Step 2: Read length and width of
a rectangle as L and W.
 Step 3: Compute area and store in A
 Step 4 : Display A
 Step 5: Stop
Examples of Algorithm:
 Example 6 : Foot to CM.
 Step 1: Start
 Step 2: Read length in feet as Lft.
 Step 3: Compute length in centimeter
as Lcm=30*Lft.
 Step 4 : Print / Display Lcm.
 Step 5: Stop
Examples of Algorithm:
 Example 7 : Fahrenheit to Celsius.
 Step 1: Start
 Step 2: Read temperature in Fahrenheit F.
 Step 3: Compute temperature in Celsius
as C = 5/9*(F-32).
 Step 4 : Print / Display C.
 Step 5: Stop
Examples of Algorithm:
 Example 8 : To find even or odd
number.
 Step 1: Start
 Step 2: Read a number
 Step 3: If number is divisible by 2
then Go to Step 4
otherwise Go to Step 5.
 Step 4: Display Even and stop.
 Step 5: Display Odd and stop.
 Step 6: Stop
Sum of First n- natural numbers
12 + 22 + 32 + 42 + ⋯ … . . +𝑛2
SUM = 1*2 + 2*3 + 3*4 + to N terms
Example :
 Example 9: Write an algorithm and draw a flowchart to find even
numbers between 0 and 99.
 Example 11: Write an algorithm and draw a flowchart to generate
the even numbers between 1000 and 2000 and then prints them in
a standard output. It should also prints the total sum of the even
numbers.
 Example 12: Write an algorithm and draw a flowchart to find odd
numbers between 0 and 99.
 Example 13: Write an algorithm and draw a flowchart to generate
the odd numbers between 1000 and 2000 and then prints them in a
standard output. It should also prints the total sum of the odd
numbers.
Examples of Algorithm:
 Example 5 : Even number in a range.
 Step 1: Start
 Step 2: Read the first even number
as EvenNum = 0.
 Step 3: Display EvenNum
 Step 4 : EvenNum = EvenNum+2
 Step 5: If (EvenNum < 100) then go to
Step 3.
else Stop.
Example :
 Example 14: Write an algorithm and draw a flowchart to
exchange the values of two variables or to swap two integers.
 Example 15: Given three numbers a, b and c. Write an
algorithm and design a flowchart to interchange the value of
a to b, b to c and c to a without using any extra variable.
 Example 16: Write an algorithm and draw a flowchart to
print the number from 1 to 10.
 Example 17: Write an algorithm and draw a flowchart to find
the sum of first ‘n’ numbers.
Example :
 Example 17: Write an algorithm and draw a flowchart to
print “Hello World” 10 times.
 Example 18: Write an algorithm and draw a flowchart to find
the sum of first 50 numbers.
 Example 19: Write an algorithm and draw a flowchart to find
the factorial of a number.
 Example 20: Write an algorithm and draw a flowchart to find
the sum of individual digits of a given number.
Factorial of a number :
Factorial of a number : Alternative Solution
Digit sum:
Example :
 Example 25: Write an algorithm and draw a flowchart to find
the reverse of a number.
Printing Reverse of a number:
 Write an algorithm and draw a flowchart to find whether the given
number is the Armstrong number or not. (num =153 is armstrong
number as 13 + 53 + 33 = 153).
 Write an algorithm and draw a flowchart to check whether the given
number is perfect number or not. The perfect number is one that equals
to the sum of its divisors (excluding number itself).
28 = 1+2+4+7+14 ; 6 = 1+2+3.
 Write an algorithm and draw a flowchart to check whether the given
number is palindrome or not. The number is Palindrome number if the
reverse of number is same as that of number.
 Write an algorithm and draw a flowchart to check whether the given
number is prime number or not.
Example :
 The current year and the year in which the employee joined
the organization are entered through the keyboard. If the
number of years for which the employee has served the
organization is greater than 3, then a bonus of Rs. 2500/- is
given to the employee. If the years of service are not greater
than 3, then the program should do nothing. Write an
algorithm and draw a flowchart for this problem.
Example :
 While purchasing certain items, a discount of 10% is offered if
the quantity purchased is more than 1000. If the quantity and
price per item are input through the keyboard, write an
algorithm and draw a flowchart to calculate the total
expenses.
Example :
 In a company an employee is paid as under: If his basic salary
is less than Rs. 150000, then HRA = 20% of basic salary and
DA = 95% of basic salary. If his salary either equal to or above
Rs.150000, then HRA = Rs.25000 and DA = 87% of basic salary.
If the employee’s salary is input through the keyboard write
an algorithm and draw a flow chart to find his gross salary.
 *Note : GrossSalary = BasicSalary + HRA + DA
Example :
 Step 1: Start
 Step 2: Initialize/Declare BS, HRA, DA, GS = 0;
 Step 3: Read the basic salary (BS) of an employee.
 Step 4:If (BS < 150000) then go to Step5 otherwise go to Step 6
 Step 5: Compute HRA = BS *(20/100 )and DA= BS * (95/100).
 Step 6: Compute HRA = 25000 and DA = BS * (87/100).
 Step 7 : Compute GS= BS + HRA +DA.
 Step 8 : Print / Display GS.
 Step 9 : Stop
Example :
 In a company an employee is paid as under: If his basic salary
is less than Rs. 1500, then HRA = 10% of basic salary and DA =
90% of basic salary. If his salary either equal to or above
Rs.1500, then HRA = Rs.500 and DA = 98% of basic salary. If
the employee’s salary is input through the keyboard write an
algorithm and draw a flow chart to find his gross salary.
*Note : GrossSalary = BasicSalary + HRA + DA
Example :
 The current year and the year in which the employee joined
the organization are entered through the keyboard. If the
number of years for which the employee has served the
organization is greater than 30, then a bonus of Rs. 50000/- is
given to the employee. If the years of service is greater than
20 and less than or equal to 30, then a bonus of Rs. 30000/- is
given to the employee. If the years of service is greater than
10 and less than or equal to 20, then a bonus of Rs. 20000/- is
given to the employee. If year of service is less than or equal
to 10 Years then, the program should do nothing. Write an
algorithm and draw a flowchart to determine the bonus
received by the employee.
 Step 1 : START
 Step 2: Read the value of Current Year (CY) and Year of Joining (YoJ).
 Step 3: Compute Year of Service YoS = CY-YoJ.
 Step 4: if (YoS > 30) then goto Step 5 otherwise go to Step 6
 Step 5: Print/Display Bonus of Rs. 50000/- .
 Step 6: if (YoS > 20 && YoS <=30) then goto Step 7 otherwise goto Step 8
 Step 7: Print/Display Bonus of Rs. 30000/-
 Step 8: if (YoS > 10 && YoS <=20) then goto Step 9 otherwise goto Step 10
 Step 9: Print/Display Bonus of Rs. 20000/-
 Step 10: STOP
Thank You..!!

You might also like