Lecture 4
Lecture 4
Lecture 4
IIT (BHU)
How to add n numbers?
▷ Concept of loop – repeat a portion of
pseudocode/code a finite number of times
▷ Series 0,1,1,2,3,5,8,13,21,…
▷ Can you guess the pattern ?
▷ Sum of previous two numbers, starts with 0 and 1
▷ Or we have, = +with = 0 and = 1
▷ We write and algorithm to compute nth term of
the Fibonacci series
Algorithm to compute nth Fibonacci term
Input: Positive number n
Output: The nth term the Fibonacci series
▷ Start
▷ Get number n
▷ Let = 0 and = 1
▷ For i = 2 up to n repeat
○ Let = +
▷ Print
▷ Stop
An Algorithm
Input: An integer limit
Output: Two integers: number and sum
1. Enter Limit
2. Set Number = 0.
3. Set Sum = 0.
4. Repeat the following:
a. If Sum > Limit, terminate the repetition,
otherwise.
b. Increment Number by one.
c. Add Number to Sum and set equal to Sum.
5. Print Number and Sum.
What does this pseudo-code do?
Paradigms
Declarative
▷
Do not state the order in which to execute
operations
▷
Supply a number of operations that are available
along with the conditions under which each is
allowed to execute.
▷
Implementation tracks which operations are free
to execute and chooses the order on its own
Imperative
▷
State the order in which operations occur, with
constructs that explicitly control that order
▷
State can be modified at one point in time, within
one unit of code, later read at a different point in
Further Paradigms
Procedural – groups code into functions - C
Functional – programs are treated as stateless
function evaluations - Haskell
Object-oriented - groups code together with the
state the code modifies - Java
Logic - computation as automated reasoning over
a body of knowledge - prolog
Symbolic - programs able to manipulate formulas
and program components as data. Programs can
thus effectively modify themselves and appear to
learn - Lisp