Algorithm & Flowchart ManualALGORITHM & FLOWCHART MANUALforSTUDENTS(Ravi K. Walia)Assistant Professor & InchargeComputer & Instrumentation CentreDr. Y. S. Parmar University of Horticulture & Forestry,Nauni Solan INDIA (HP)1CIC-UHF

Algorithm & Flowchart ManualPREFACEThis document has been prepared for students at Dr. Y. S. Parmar University of Horticulture& Forestry, Nauni, Solan (HP) India. Software Engineer uses various programminglanguages to create programs. Before writing a program, first needs to find a procedure forsolving the problem. The program written without proper pre-planning has higher chances oferrors.Algorithm and flowchart are the powerful tools for learning programming. An algorithm is astep-by-step analysis of the process, while a flowchart explains the steps of a program in agraphical way. Algorithm and flowcharts helps to clarify all the steps for solving the problem.For beginners, it is always recommended to first write algorithm and draw flowchart forsolving a problem and then only write the program.Beginners find it difficult to write algorithm and draw flowchart. The algorithm can vary fromperson to person to solve a particular problem. The manual will be useful for the students tolearn algorithm and flowchart. It includes basics of algorithm and flowchart along withnumber of examples. Software ClickCharts by NCH (unlicensed version) has been used todraw all the flowcharts in the manual.2CIC-UHF

Algorithm & Flowchart Manual.ALGORITHM:The word “algorithm” relates to the name of the mathematician Al-khowarizmi, which meansa procedure or a technique. Software Engineer commonly uses an algorithm for planningand solving the problems. An algorithm is a sequence of steps to solve a particular problemor algorithm is an ordered set of unambiguous steps that produces a result and terminates ina finite timeAlgorithm has the following characteristics Input: An algorithm may or may not require input Output: Each algorithm is expected to produce at least one result Definiteness: Each instruction must be clear and unambiguous. Finiteness: If the instructions of an algorithm are executed, the algorithmshould terminate after finite number of stepsThe algorithm and flowchart include following three types of control structures.1. Sequence: In the sequence structure, statements are placed one after the other andthe execution takes place starting from up to down.2. Branching (Selection): In branch control, there is a condition and according to acondition, a decision of either TRUE or FALSE is achieved. In the case of TRUE, oneof the two branches is explored; but in the case of FALSE condition, the otheralternative is taken. Generally, the ‘IF-THEN’ is used to represent branch control.3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be executedrepeatedly based on certain loop condition e.g. WHILE, FOR loops.Advantages of algorithm It is a step-wise representation of a solution to a given problem, which makes it easyto understand. An algorithm uses a definite procedure. It is not dependent on any programming language, so it is easy to understand foranyone even without programming knowledge. Every step in an algorithm has its own logical sequence so it is easy to debug.3CIC-UHF

Algorithm & Flowchart ManualHOW TO WRITE ALGORITHMSStep 1 Define your algorithms input: Many algorithms take in data to be processed, calculate the area of rectangle input may be the rectangle height and rectangle width.Step 2 Define the variables: Algorithm's variables allow you to use it for more than oneplace. We can define two variables for rectangle height and rectangle width as HEIGHT andWIDTH (or H & W). We should use meaningful variable name e.g. instead of using H & Wuse HEIGHT and WIDTH as variable name.Step 3 Outline the algorithm's operations: Use input variable for computation purpose,e.g. to find area of rectangle multiply the HEIGHT and WIDTH variable and store the value innew variable (say) AREA. An algorithm's operations can take the form of multiple steps andeven branch, depending on the value of the input variables.Step 4 Output the results of your algorithm's operations: In case of area of rectangleoutput will be the value stored in variable AREA. if the input variables described a rectanglewith a HEIGHT of 2 and a WIDTH of 3, the algorithm would output the value of 6.FLOWCHART:The first design of flowchart goes back to 1945 which was designed by John Von Neumann.Unlike an algorithm, Flowchart uses different symbols to design a solution to a problem. It isanother commonly used programming tool. By looking at a Flowchartone can understand theoperations and sequence of operations performed in a system. Flowchart is often consideredas a blueprint of a design used for solving a specific problem.Advantages of flowchart: Flowchart is an excellent way of communicating the logic of a program. Easy and efficient to analyze problem using flowchart. During program development cycle, the flowchart plays the role of a blueprint, whichmakes program development process easier. After successful development of a program, it needs continuous timely maintenanceduring the course of its operation. The flowchart makes program or systemmaintenance easier. It is easy to convert the flowchart into any programming language code.4CIC-UHF

Algorithm & Flowchart ManualFlowchart is diagrammatic /Graphical representation of sequence of steps to solve aproblem. To draw a flowchart following standard symbols are useSymbol NameSymbolfunctionUsed to represent start andend of flowchartOvalParallelogramUsed for input and outputoperationRectangleProcessing: Used forarithmetic operations anddata-manipulationsDiamondDecision making. Used torepresent the operation inwhich there are two/threealternatives, true and falseetcArrowsFlow line Used to indicatethe flow of logic byconnecting symbolsCirclePage ConnectorOff Page ConnectorPredefined Process/Function Used to representa group of statementsperforming one processingtask.Preprocessor ---------------------- --------------5CommentsCIC-UHF

Algorithm & Flowchart ManualThe language used to write algorithm is simple and similar to day-to-day life language. Thevariable names are used to store the values. The value store in variable can change in thesolution steps. In addition some special symbols are used as belowAssignment Symbol ( or ) is used to assign value to the variable.e.g. to assign value 5 to the variable HEIGHT, statement isHEIGHT 5orHEIGHT 5The symbol ‘ ’ is used in most of the programming language as an assignment symbol, thesame has been used in all the algorithms and flowcharts in the manual.The statement C A Bmeans that add the value stored in variable A and variable Bthen assign/store the value in variable C.The statement R R 1 means that add I to the value stored in variable R and thenassign/store the new value in variable R, in other words increase the value of variable R by 1Mathematical Operators:OperatorMeaningExample AdditionA B-SubtractionA–B*MultiplicationA*B/DivisionA/ B PowerA 3 for A3%ReminderA%BOperatorMeaningExample Less thanA B Less than or equal toA B or Equal toA B# or ! Not equal toA # B or A ! B Greater thanA B Greater tha or equal toA BRelational Operators6CIC-UHF

Algorithm & Flowchart ManualLogical OperatorsOperatorExampleMeaningANDA B AND B CResult is True if both A B andB C are true else falseORA B OR B CResult is True if either A B orB C are true else falseNOTNOT (A B)Result is True if A B is falseelse trueSelection control StatementsSelection ControlExampleMeaningIF ( Condition ) Then ENDIFIF ( X 10 ) THENY Y 5ENDIFIf condition X 10 is Trueexecute the statementbetween THEN and ENDIFIF ( Condition ) Then ELSE .IF ( X 10 ) THENY Y 5ELSEY Y 8Z Z 3ENDIFIf condition X 10 is Trueexecute the statementbetween THEN and ELSEotherwise execute thestatements between ELSEand ENDIFSelection ControlExampleMeaningWHILE (Condition)DO.ENDDODO . UNTILL (Condition)WHILE ( X 10)DOprint xx x 1ENDDODOprint xx x 1UNTILL ( X 10)Execute the loop as long asthe condition is TRUEENDIFLoop control StatementsExecute the loop as long asthe condition is falseGO TO statement also called unconditional transfer of control statement is used to transfercontrol of execution to another step/statement. . e.g. the statement GOTO n will transfercontrol to step/statement n.Note: We can use keyword INPUT or READ or GET to accept input(s) /value(s) andkeywords PRINT or WRITE or DISPLAY to output the result(s).7CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find the sum of two numbersAlgorithmStep-1 StartStep-2Input first numbers say AStep-3Input second number say BStep-4SUM A BStep-5Display SUMStep-6 StopORAlgorithmStep-1 StartStep-2Input two numbers say A & BStep-3SUM A BStep-4Display SUMStep-5 Stop8CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to convert temperature from Celsius to FahrenheitC : temperature in CelsiusF : temperature FahrenheitAlgorithmStep-1 StartStep-2Input temperature in Celsius say CStep-3F (9.0/5.0 x C) 32Step-4Display Temperature in Fahrenheit FStep-5 StopAlgorithm & Flowchart to convert temperature from Fahrenheit to CelsiusC : temperature in CelsiusF : temperature FahrenheitAlgorithmStep-1 StartStep-2Input temperature in Fahrenheit say FStep-3C 5.0/9.0 (F - 32 )Step-4Display Temperature in Celsius CStep-5 Stop9CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find Area and Perimeter of SquareL : Side Length of SquareAREA : Area of SquarePERIMETER : Perimeter of SquareAlgorithmStep-1 StartStep-2Input Side Length of Square say LStep-3Area L x LStep-4PERIMETER 4 x LStep-5Display AREA, PERIMETERStep-6 StopAlgorithm & Flowchart to find Area and Perimeter of RectangleL : Length of RectangleB : Breadth of RectangleAREA : Area of RectanglePERIMETER : Perimeter of RectangleAlgorithmStep-1 StartStep-2Input Side Length & Breadth say L, BStep-3Area L x BStep-4PERIMETER 2 x ( L B)Step-5Display AREA, PERIMETERStep-6 Stop10CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find Area and Perimeter of CircleR : Radius of CircleAREA : Area of CirclePERIMETER : Perimeter of CircleStartInput Valueof RAlgorithmStep-1 StartStep-2Input Radius of Circle say RStep-3Area 22.0/7.0 x R x RStep-4PERIMETER 2 x 22.0/7.0 x RStep-5Display AREA, PERIMETERAREA 22.0/7.0xRx RPERIMTER 2 X22.0/7.0 x RPr int A REA,PER I M T ERStep-6 StopStopAlgorithm & Flowchart to find Area & Perimeter of Triangle(when three sides are given)A : First Side of TriangleB : Second Side of TriangleC : Third Side of TriangleAREA : Area of TrianglePERIMETER : Perimeter of TriangleAlgorithmStep-1 StartStep-2Input Sides of Triangle A,B,CStep-3S (A B C)/ 2.0Step-4AREA SQRT(S x (S-A) x (S-B) x(S-C))Step-5PERIMETER S1 S2 S3Step-6Display AREA, PERIMETERStep-7 Stop11CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find Simple InterestP : Principle AmountN : Time in YearsR : % Annual Rate of InterestSI : Simple InterestAlgorithmStep-1 StartStep-2Input value of P, N, RStep-3SI (P x N x R)/100.0Step-4Display SI FStep-6 StopAlgorithm & Flowchart to find Compound InterestP : Principle AmountN : Time in YearsR : % Annual Rate of InterestCI : Compound InterestAlgorithmStep-1 StartStep-2Input value of P, N, R CStep-3CI P(1 R/100)N - PStep-4Display CIStep-6 Stop.12CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to Swap Two Numbers using Temporary VariableAlgorithmStep-1 StartStep-2Input Two Numbers Say NUM1,NUM2Step-3Display Before Swap Values NUM1, NUM2Step-4TEMP NUM1Step-5NUM1 NUM2Step-6NUM2 NUM1Step-7Display After Swap Values NUM1,NUMStep-8 StopAlgorithm & Flowchart to Swap Two Numbers without using temporaryvariableAlgorithmStep-1 StartStep-2Input Two Numbers Say A,BStep-3Display Before Swap Values A, BStep-4A A BStep-5B A-BStep-6A A-BStep-7Display After Swap Values A, BStep-8 Stop13CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find the smallest of two numbersAlgorithmStep-1 StartStep-2 Input two numbers sayNUM1,NUM2Step-3 IF NUM1 NUM2 THENprint smallest is NUM1ELSEprint smallest is NUM2ENDIFStep-4 StopAlgorithm & Flowchart to find the largest of two numbersAlgorithmStartStep-1 StartStep-2 Input two numbers sayI nput V alueof NUM1NUM1,NUM2Step-3IF NUM1 NUM2 THENprint largest is NUM1Input Valueof NUM2ELSEprint largest is NUM2PrintLargest isNUM1ENDIFStep-4YesifNUM1 NUM2NoPrintLargest isNUM2StopStop14CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find the largest of three numbersAlgorithmStep-1 StartStep-2 Read three numbers say num1,num2, num3Step-3if num1 num2 then go to step-5Step-4IF num2 num3 THENprint num2 is largestELSEprint num3 is largestENDIFGO TO Step-6Step-5 IF num1 num3 THENprint num1 is largestELSEprint num3 is largestENDIFStep-6 Stop15CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find the largest of three numbers (an another way)AlgorithmStep-1 StartStep-2 Read three numbers say A,B,CStep-3BIG AStep-4IF B BIG THENBIG BENDIFStep-5IF C BIG THENBIG CENDIFStep-6Write BIGStep-7Stop16CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find Even number between 1 to 50AlgorithmStep-1 StartStep-2I 1Step-3IF (I 50) THENGO TO Step-7ENDIFStep-4IF ( (I % 2) 0) THENDisplay IENDIFStep-5I I 1Step-6GO TO Step--3Step-7 StopAlgorithm & Flowchart to find Odd numbers between 1 to n where n is apositive IntegerAlgorithmStep-1 StartStep-2 Input Value of NStep-3I 1Step-4IF (I N) THENGO TO Step-8ENDIFStep-5IF ( (I % 2) 1) THENDisplay IENDIFStep-6I I 1Step-7GO TO Step-4Step-8Stop17CIC-UHF

Algorithm & Flowchart Manual.Algorithm & Flowchart to find sum of series 1 2 3 . NAlgorithmStep-1 StartStep-2 Input Value of NStep-3I 1, SUM 0Step-4IF (I N) THENGO TO Step-8ENDIFStep-5SUM SUM IStep-6I I 1Step-7Go to step-4Step-8Display