You are on page 1of 24

What is Algorithm?

It is a sequence of instructions to be carried out in order to solve a specific problem. In designing a solution, we write step by step procedures for solving the problem. Such process is called algorithm. Algorithm can be written in any language according to the convenience of the program developer.

To add two numbers.


Read the Value of A and B. SUM = A+B. Display SUM. Stop.

To Multiply two numbers.


Read the Value of A and B. PRODUCT = A*B. Display PRODUCT. Stop.

1. Start 2. sum = 0 3. Get the first number 4. Add first number to sum (sum= sum+ first number) 5. Get the second number 6. Add to sum 7. Get the third number 8. Add to sum 9. Get the Forth number 10. Add to sum 11. Get the fifth number 12. Add to sum 13. Get the sixth number 14. Add to sum 15. Output the sum

Example : Write an algorithm to determine a students final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. Start Step 1: Input M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print FAIL Step 4: if (GRADE < 50) then Print PASS Stop

Write an algorithm and draw a flowchart to find Greatest of three numbers.


Algorithm Start Step1: Read A, B, C Step2: If A > B is True, then check whether A > C, if yes then Print A is greatest otherwise C is greatest. Step3: If A > B is False, then check whether B > C, if yes then Print B is greatest otherwise C is greatest. Stop.

characteristics of algorithm
1. 2. Finiteness : An algorithm must terminate after a finite number of steps and further each steps must be executable in finite amount of time. Each step of an algorithm must be precisely defined; the action to be carried out must be rigorously and unambiguously specified for each case. Input: An algorithm has zero or more, but only finite number of inputs. zero input example: ASCII chart of 0-255 Output: An algorithm has one or more output. Effectiveness: Should be effective that means each of the operation to be performed in an algorithm must be sufficiently basic that it can, in principle, be done exactly and in a finite length of time, by a person using pencil and paper and should be computer programming language independent

3. 4. 5.

The Flowchart
A Flowchart
shows logic of an algorithm emphasizes individual steps and their interconnections e.g. control flow from one action to the next

INTRODUCTION FLOWCHART
The flowchart is a means of visually presenting the flow of data, the operations performed and the sequence in which they are performed to solve a problem. A flowchart is a diagrammatic representation that represents the sequence of operations to be performed to get the solution of a problem. Flowcharts are generally drawn in the early stages of formulating computer solutions.

INTRODUCTION FLOWCHART
These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. flowcharts are helpful in explaining the program to others.

Flowcharts are usually drawn using some standard symbols;


1. 2. Start or end of the program Computational steps or processing function of a program Input or output operation Decision making and branching Connector or joining of two parts of program

3. 4. 5.

Flowcharts symbols

6.Magnetic Tape 7.Magnetic Disk 8.Offpageconnector 9.Flow line

Flowchart Symbols
Basic
Name Symbol Use in Flowchart Oval Denotes the beginning or end of the program

Parallelogram

Denotes an input operation

Rectangle

Denotes a process to be carried out e.g. addition, subtraction, division etc.

Diamond

Denotes a decision (or branch) to be made. The program should continue along one of two routes. (e.g. IF/THEN/ELSE)

Hybrid

Denotes an output operation

Flow line

Denotes the direction of logic flow in the program

Example
Draw a flowchart to find the sum of first 50 natural numbers. Algorithm Start. Initialize n and sum to zero(n=0;sum=0). Add 1 to n(n=n+1). Add n to sum(sum=sum+n). Is n=50? If no go to step 3. Print sum. Stop.

Advantages of Using Flowchart


Communication: Flowcharts are better way of communicating the logic of a system to all concerned. Effective analysis: With the help of flowchart, problem can be analyzed in more effective way. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. Proper Debugging: The flowchart helps in debugging process.

LIMITATIONS OF USING FLOWCHARTS


Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. Alterations and Modifications: If alterations are required the flowchart may require redrawing completely. The essentials of what is done can easily be lost in the technical details of how it is done.

Example :Draw a flowchart to determine a students final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.
START

Input M1,M2,M3,M4

Step 1: Step 2: Step 3:

GRADE(M1+M2+M3+M4)/4

Input M1,M2,M3,M4 GRADE (M1+M2+M3+M4)/4 if (GRADE <50) then Print FAIL else Print PASS endif

IS GRADE<50

PRINT PASS

PRINT FAIL

STOP

Example 3
Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.

Example 3
Algorithm Step 1: Input W,L Step 2: A L x W Step 3: Print A
START

Input W, L

ALxW

Print A

STOP

Example 4
Write an algorithm and draw a flowchart to find Greatest of three numbers. Algorithm Start Step1: Read A, B, C Step2: If A > B is True, then check whether A > C, if yes then Print A is greatest otherwise C is greatest. Step3: If A > B is False, then check whether B > C, if yes then Print B is greatest otherwise C is greatest. Stop.

To find volume and surface area of a cylinder

You might also like