Algorithm for C Programs, Lecture notes of C programming

Algorithm for C Programs,, it gives practical explanation

Typology: Lecture notes

2019/2020

Uploaded on 09/24/2020

venkatme83
venkatme83 🇮🇳

5 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm & Flowchart Manual
1 CIC-UHF
ALGORITHM & FLOWCHART MANUAL
for
STUDENTS
(Ravi K. Walia)
Assistant Professor & Incharge
Computer & Instrumentation Centre
Dr. Y. S. Parmar University of Horticulture & Forestry,
Nauni Solan INDIA (HP)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Algorithm for C Programs and more Lecture notes C programming in PDF only on Docsity!

ALGORITHM & FLOWCHART MANUAL

for

STUDENTS

(Ravi K. Walia) Assistant Professor & Incharge Computer & Instrumentation Centre Dr. Y. S. Parmar University of Horticulture & Forestry, Nauni Solan INDIA (HP)

PREFACE

This document has been prepared for students at Dr. Y. S. Parmar University of Horticulture & Forestry, Nauni, Solan (HP) India. Software Engineer uses various programming languages to create programs. Before writing a program, first needs to find a procedure for solving the problem. The program written without proper pre-planning has higher chances of errors. Algorithm and flowchart are the powerful tools for learning programming. An algorithm is a step-by-step analysis of the process, while a flowchart explains the steps of a program in a graphical 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 for solving a problem and then only write the program. Beginners find it difficult to write algorithm and draw flowchart. The algorithm can vary from person to person to solve a particular problem. The manual will be useful for the students to learn algorithm and flowchart. It includes basics of algorithm and flowchart along with number of examples. Software ClickCharts by NCH (unlicensed version) has been used to draw all the flowcharts in the manual.

HOW TO WRITE ALGORITHMS

Step 1 Define your algorithms input : Many algorithms take in data to be processed, e.g. to 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 one place. We can define two variables for rectangle height and rectangle width as HEIGHT and WIDTH (or H & W). We should use meaningful variable name e.g. instead of using H & W use 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 in new variable (say) AREA. An algorithm's operations can take the form of multiple steps and even branch, depending on the value of the input variables. Step 4 Output the results of your algorithm's operations : In case of area of rectangle output will be the value stored in variable AREA. if the input variables described a rectangle with 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 is another commonly used programming tool. By looking at a Flowchartone can understand the operations and sequence of operations performed in a system. Flowchart is often considered as 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, which makes program development process easier.
  • After successful development of a program, it needs continuous timely maintenance during the course of its operation. The flowchart makes program or system maintenance easier.
  • It is easy to convert the flowchart into any programming language code.

Flowchart is diagrammatic /Graphical representation of sequence of steps to solve a problem. To draw a flowchart following standard symbols are use Symbol Name Symbol function Oval Used to represent start and end of flowchart Parallelogram Used for input and output operation Rectangle Processing: Used for arithmetic operations and data-manipulations Diamond Decision making. Used to represent the operation in which there are two/three alternatives, true and false etc Arrows Flow line Used to indicate the flow of logic by connecting symbols Circle Page Connector Off Page Connector Predefined Process /Function Used to represent a group of statements performing one processing task. Preprocessor

--------------
--------------

Comments

Logical Operators Operator Example Meaning AND A < B AND B < C Result is True if both A<B and B<C are true else false OR A< B OR B < C Result is True if either A<B or B<C are true else false NOT NOT (A >B) Result is True if A>B is false else true Selection control Statements Selection Control Example Meaning IF ( Condition ) Then … ENDIF

IF ( X > 10 ) THEN

Y=Y+

ENDIF

If condition X>10 is True execute the statement between THEN and ENDIF IF ( Condition ) Then … ELSE ….. ENDIF

IF ( X > 10 ) THEN

Y=Y+

ELSE

Y=Y+

Z=Z+

ENDIF

If condition X>10 is True execute the statement between THEN and ELSE otherwise execute the statements between ELSE and ENDIF Loop control Statements Selection Control Example Meaning WHILE (Condition) DO .. .. ENDDO

WHILE ( X < 10)

DO

print x x=x+ ENDDO Execute the loop as long as the condition is TRUE DO …. … UNTILL (Condition)

DO

print x x=x+ UNTILL ( X >10) Execute the loop as long as the condition is false GO TO statement also called unconditional transfer of control statement is used to transfer control of execution to another step/statement.. e.g. the statement GOTO n will transfer control to step/statement n. Note: We can use keyword INPUT or READ or GET to accept input(s) /value(s) and keywords PRINT or WRITE or DISPLAY to output the result(s).

Algorithm & Flowchart to find the sum of two numbers

Algorithm Step- 1 Start Step- 2 Input first numbers say A Step- 3 Input second number say B Step- 4 SUM = A + B Step- 5 Display SUM Step- 6 Stop OR Algorithm Step- 1 Start Step- 2 Input two numbers say A & B Step- 3 SUM = A + B Step- 4 Display SUM Step- 5 Stop

Algorithm & Flowchart to find Area and Perimeter of Square

L : Side Length of Square AREA : Area of Square PERIMETER : Perimeter of Square Algorithm Step- 1 Start Step- 2 Input Side Length of Square say L Step- 3 Area = L x L Step- 4 PERIMETER = 4 x L Step- 5 Display AREA, PERIMETER Step- 6 Stop

Algorithm & Flowchart to find Area and Perimeter of Rectangle

L : Length of Rectangle B : Breadth of Rectangle AREA : Area of Rectangle PERIMETER : Perimeter of Rectangle Algorithm Step- 1 Start Step- 2 Input Side Length & Breadth say L, B Step- 3 Area = L x B Step- 4 PERIMETER = 2 x ( L + B) Step- 5 Display AREA, PERIMETER Step- 6 Stop

Algorithm & Flowchart to find Area and Perimeter of Circle

R : Radius of Circle AREA : Area of Circle PERIMETER : Perimeter of Circle Algorithm Step- 1 Start Step- 2 Input Radius of Circle say R Step- 3 Area = 22.0/7.0 x R x R Step- 4 PERIMETER = 2 x 22.0/7.0 x R Step- 5 Display AREA, PERIMETER Step- 6 Stop

Algorithm & Flowchart to find Area & Perimeter of Triangle

(when three sides are given)

A : First Side of Triangle B : Second Side of Triangle C : Third Side of Triangle AREA : Area of Triangle PERIMETER : Perimeter of Triangle Algorithm Step- 1 Start Step- 2 Input Sides of Triangle A,B,C Step- 3 S= (A + B + C)/ 2. Step- 4 AREA = SQRT(S x (S-A) x (S-B) x(S-C)) Step- 5 PERIMETER = S1 + S2 + S Step- 6 Display AREA, PERIMETER Step- 7 Stop Start Input Value of R AREA = 22.0/7. x R x R Pr int A REA, PER IM T ER Stop PERIMTER = 2 X 22.0/7.0 x R

Algorithm & Flowchart to Swap Two Numbers using Temporary Variable

Algorithm Step- 1 Start Step- 2 Input Two Numbers Say NUM1,NUM Step- 3 Display Before Swap Values NUM1, NUM Step- 4 TEMP = NUM Step- 5 NUM1 = NUM Step- 6 NUM2 = NUM Step- 7 Display After Swap Values NUM1,NUM Step- 8 Stop

Algorithm & Flowchart to Swap Two Numbers without using temporary

variable

Algorithm Step- 1 Start Step- 2 Input Two Numbers Say A,B Step- 3 Display Before Swap Values A, B Step- 4 A = A + B Step- 5 B = A - B Step- 6 A = A - B Step- 7 Display After Swap Values A, B Step- 8 Stop

Algorithm & Flowchart to find the smallest of two numbers

Algorithm Step- 1 Start Step- 2 Input two numbers say NUM1,NUM Step- 3 IF NUM1 < NUM2 THEN print smallest is NUM ELSE print smallest is NUM ENDIF Step- 4 Stop

Algorithm & Flowchart to find the largest of two numbers

Algorithm Step- 1 Start Step- 2 Input two numbers say NUM1,NUM Step- 3 IF NUM1 > NUM2 THEN print largest is NUM ELSE print largest is NUM ENDIF Step- 4 Stop Start I nput V alue of NUM Input Value of NUM Stop if NUM1 > NUM Print Largest is NUM Print Largest is NUM Yes No

Algorithm & Flowchart to find the largest of three numbers (an another way)

Algorithm Step- 1 Start Step- 2 Read three numbers say A,B,C Step- 3 BIG = A Step- 4 IF B > BIG THEN BIG = B ENDIF Step- 5 IF C >BIG THEN BIG = C ENDIF Step- 6 Write BIG Step- 7 Stop

Algorithm & Flowchart to find Even number between 1 to 50

Algorithm Step- 1 Start Step- 2 I = 1 Step- 3 IF (I >50) THEN GO TO Step- 7 ENDIF Step- 4 IF ( (I % 2) =0) THEN Display I ENDIF Step- 5 I = I + 1 Step- 6 GO TO Step-- 3 Step- 7 Stop

Algorithm & Flowchart to find Odd numbers between 1 to n where n is a

positive Integer

Algorithm Step- 1 Start Step- 2 Input Value of N Step- 3 I = 1 Step- 4 IF (I >N) THEN GO TO Step- 8 ENDIF Step- 5 IF ( (I % 2)=1) THEN Display I ENDIF Step- 6 I = I + 1 Step- 7 GO TO Step- 4 Step- 8 Stop

Algorithm & Flowchart to find sum of series 1 – X + X^2 – X^3 ….XN

Algorithm Step- 1 Start Step- 2 Input Value of N, X Step- 3 I = 1, SUM=1, TERM= Step- 4 IF (I >N) THEN GO TO Step- 9 ENDIF Step- 5 TERM = - TERM * X Step- 6 SUM = SUM + TERM Step- 7 I = I + 1 Step- 8 Go to step- 4 Step- 9 Display value of SUM Step- 10 Stop

Algorithm & Flowchart to print multiplication Table of a number

Algorithm Step- 1 Start Step- 2 Input Value of NUM Step- 3 I = 1 Step- 4 IF (I >10) THEN GO TO Step 9 ENDIF Step- 5 PROD = NUM * I Step- 6 WRITE I “x” NUM “=” PROD Step- 7 I = I + 1 Step- 8 Go to step- 4 Step- 9 Stop

Algorithm & Flowchart to generate first n Fibonacci terms 0,1,1,2,3,5…n (n>2)

Algorithm Step- 1 Start Step- 2 Input Value of N Step- 3 A=0, B=1, COUNT= Step- 4 WRITE A, B Step- 5 IF (COUNT >N) then go to step 12 Step- 6 NEXT= A + B Step- 7 WRITE NEXT Step- 8 A=B Step- 9 B=NEXT Step- 10 COUNT=COUNT + 1 Step- 11 Go to step- 4 Step- 12 Stop