1.Basics of algorithms-flowchart
1.Basics of algorithms-flowchart
2
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Session Plan
The prerequisite will cover following topics:-
• What is an algorithm?
• What pseudocode?
• What is flowchart?
3
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Algorithm
Representation of a solution to any problem is termed as an
algorithm.
It is not written specific to any Programming Language.
Algorithms can be written in the form of:
• Flow chart (pictorial representation)
• Pseudocode.
4
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Flowchart
• It is a graphical representation of a procedure or algorithm
• It illustrates a complex process into a simple diagrams using a flowchart
5
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?
• Effective documentation
6
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?
• Effective documentation
7
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?
• Effective documentation
8
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?
• Effective documentation
9
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Symbols used in flowchart
10
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Pseudo Code Writing: Industry Pattern
12
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Conditional Statements
• IF is written followed by
condition followed by THEN.
• In case of false condition ELSE
can be written.
13
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Loops
14
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Loops( continued…)
15
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Loops( continued…)
16
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: To Print even or odd
ALGORITHM EvenOrOdd()
INPUT: An integer Number
OUTPUT: Nature of Number, i.e., Even or Odd
BEGIN:
READ(N)
IF N%2 = = 0 THEN
WRITE(“EVEN NUMBER”);
ELSE
WRITE(“ODD NUMBER”);
END
17
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: Algorithm to find leap year
ALGORITHM LeapOrNonLeap(year)
INPUT: An year
OUTPUT: Nature of year, i.e., Leap or Non-Leap
BEGIN:
IF year%4 = = 0 AND year%100!=0 OR year%400 = = 0 THEN
WRITE(“Leap year”)
ELSE
WRITE(“Non Leap Year”)
END;
18
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: Algorithm for printing table
ALGORITHM TableOfNumber()
INPUT: An Integer Number
OUTPUT: Table of Input Number BEGIN:
READ (N)
FOR i=1 TO 10 DO
WRITE (N*i)
END;
19
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: Algorithm to find reverse
ALGORITHM NumberReverse()
INPUT: An Integer Number
OUTPUT: Reverse of Input Number
BEGIN:
READ (N)
Rev=0
WHILE N>0 DO
Remainder=N%10
Rev = Rev*10+Remainder
N = N/10
WRITE (“Reversed Number is”)
WRITE (Rev)
END; 20
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Algorithm to find Prime
ALGORITHM PrimeNumber()
INPUT: A range of Numbers
OUTPUT: All Prime Numbers
BEGIN:
READ (LowerLimit)
READ (UpperLimit)
FOR i = LowerLimit TO UpperLimit Do
FOR j = 2 TO SQRT(i) DO
IF i%j = = 0 THEN
BREAK;
IF J=SQRT(i)+1 THEN
WRITE(i)
END; 21
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Algorithm to swap two numbers
22
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Bubble Sort
ALGORITHM BubbleSort (A[ ], N)
INPUT: A set of N Numbers
OUTPUT: Sorted Numbers
BEGIN:
FOR i =1 TO n–1
FOR j=0 TO n-i–1 DO
IF A[j]>A[j+1] THEN
CALL Exchange(A[j], A[j+1])
END;
23
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: To find factorial
ALGORITHM Factorial (N)
INPUT: An Integer Number
OUTPUT: Factorial Value of the Number
BEGIN:
IF N = 0 THEN
RETURN 1
ELSE
RETURN N*CALL Factorial(N–1)
END;
24
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: To nth Fibonacci number
ALGORITHM FibonacciNumber (N)
INPUT: A integer Numbers
OUTPUT: Nth Fibonacci Term
BEGIN:
IF N = = 1 THEN
RETURN 0
ELSE
IF N = = 2 THEN
RETURN 1
ELSE RETURN CALL Fibonacci(N–1) +
CALLFibonacci(N–2)
END 25
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Binary Search
ALGORITHM BinarySearch (A[ ], N, SearchKey)
INPUT: An Array of Size N and a search key
OUTPUT: Location of Search key in the given Array
BEGIN:
Low = 0
High = N–1
WHILE Low<= High
Mid = (Low+High)/2
IF A[Mid] = = SearchKey THEN
RETURN Mid
ELSE
IF SearchKey<A[Mid] THEN
High = Mid–1
ELSE
Low = Mid+1
RETURN –1
END; 26
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Review Questions
27
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College