Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Unit I Algorithm

Uploaded by

tv5874
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit I Algorithm

Uploaded by

tv5874
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

School of Computing Science and Engineering

Course Code : E2UC101C Name: Programming for Problem Solving

UNIT I
INTRODUCTION TO COMPUTERS AND ALGORITHMS

ALGORITHM

Name of the Faculty: Tarachand Verma Program Name:B.Tech(CSE)


Objective

 Need of Algorithm
 How to write Algorithm
 Examples

Program Name: B.Tech (CSE)


Need for Algorithm

 To write a correct program, a programmer must write each and every instruction in
the correct sequence
 Logic (instruction sequence) of a program can be very complex
 Hence, programs must be planned before they are written to ensure program
instructions are:
 Appropriate for the problem
 In the correct sequence

Program Name: B.Tech (CSE)


Algorithm

 Refers to the logic of a program and a step-by-step description of how to


arrive at the solution of a given problem
 In order to qualify as an algorithm, a sequence of instructions must
have following characteristics:
 Each and every instruction should be precise and unambiguous
 Each instruction should be such that it can be performed in a finite time
 One or more instructions should not be repeated infinitely. This ensures that the
algorithm will ultimately terminate
 After performing the instructions, that is after the algorithm terminates, the desired
results must be obtained

Program Name: B.Tech (CSE)


Example 1- Swapping of 2 Variables
Step 1:Start
Step 2:Declare variables A, B and C
Step 3:Read Value of A and B
Step 4:C=A
Step 5:A=B
Step 6:B=C
Step 7:Print A
Step 8:Print B
Step 9:Stop.

Program Name: B.Tech (CSE)


Example 2
Write an algorithm to find the summation of numbers from 0 to 15.

Step 1: Start
Step 2: I=0
Step 3: Sum=0
Step 4:Sum=Sum+I
Step 5: if I =15 then
Goto 6
else I=I+1
Goto 4
endif
Step 6: print Sum
Step 7: stop

Program Name: B.Tech (CSE)


Example 3
Write an algorithm for calculating and printing factorial (!) of a given number. ( n!=1*2*3......*n )

Step 1: Start
Step 2: Input n
Step 3: I=1
Step 4: X=1
Step 5: X=X*I
Step 6: if I = n then
Goto 7
else I=I+1
Goto 5
endif
Step 7: print X
Step 8: stop

Program Name: B.Tech (CSE)


Example 4
•There are 50 students in a class who appeared in their
final examination. Their mark sheets have been given to
you.

•The division column of the mark sheet contains the


division (FIRST, SECOND, THIRD or FAIL) obtained by the
student.

•Write an algorithm to calculate and print the total number


of students who passed in FIRST division.
Program Name: B.Tech (CSE)
Sample Algorithm
Step 1: Initialize
Total_First_Division and
Total_Marksheets_Checked to zero.
Step 2: Take the mark sheet of the next student.

Step 3: Check the division column of the mark sheet to see if it is FIRST, if yes go
to step 4 , if no, go to Step 5.

Step 4: Add 1 to Total_First_Division.

Step 5: Add 1 to Total_Marksheets_Checked.

Step 6: Is Total_Marksheets_Checked = 50, go to step 7, if no, go to Step 2. Step

7: Print Total_First_Division.

Step 8: Stop.
Program Name: B.Tech (CSE)
Example 5
There are 100 employees in an organization. The organization wants to distribute annual bonus
to the employees based on their performance. The performance of the employees is recorded in
their annual appraisal forms.
Every employee’s appraisal form contains his/her basic salary and the grade for his/her
performance during the year. The grade is of three categories – ‘A’ for outstanding performance,
‘B’ for good performance, and ‘C’ for average performance.
It has been decided that the bonus of an employee will be 100% of the basic salary for
outstanding performance, 70% of the basic salary for good performance, 40% of the basic
salary for average performance, and zero for all other cases.
Write an algorithm to calculate and print the total bonus amount to be distributed by the
organization.

Program Name: B.Tech (CSE)


Sample Algorithm
Step 1: Initialize Total_Bonus and Total_Employees_Checked to zero.

Step 2: Initialize Bonus and Basic_Salary to zero.

Step 3: Take the appraisal form of the next employee.

Step 4: Read the employee’s Basic_Salary and Grade.

Step 5: If Grade = A, then Bonus = Basic_Salary. Go to Step 8.

Step 6: If Grade = B, then Bonus = Basic_Salary x 0.7. Go to Step 8.

Step 7: If Grade = C, then Bonus = Basic_Salary x 0.4.

Step 8: Add Bonus to Total_Bonus.

Step 9: Add 1 to Total_Employees_Checked.

Step 10: If Total_Employees_Checked < 100, then go to Step 2.

Step 11: Print Total_Bonus.

Step 12: Stop.


Program Name: B.Tech (CSE)
Summary

• An algorithm is step by step instruction of how to perform s particular


task.

Program Name: B.Tech (CSE)

You might also like