0% found this document useful (0 votes)
2 views12 pages

Lecture 4

The document outlines various algorithms for basic programming tasks, including summing numbers, finding the greatest number, checking for prime numbers, calculating factorials, and generating Fibonacci series. It also discusses different programming paradigms such as declarative, imperative, procedural, functional, object-oriented, logic, and symbolic programming. Each algorithm is presented with input and output specifications, along with a step-by-step pseudocode format.

Uploaded by

bshreyas136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Lecture 4

The document outlines various algorithms for basic programming tasks, including summing numbers, finding the greatest number, checking for prime numbers, calculating factorials, and generating Fibonacci series. It also discusses different programming paradigms such as declarative, imperative, procedural, functional, object-oriented, logic, and symbolic programming. Each algorithm is presented with input and output specifications, along with a step-by-step pseudocode format.

Uploaded by

bshreyas136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CSO 101

Lecture 4
IIT (BHU)
How to add n numbers?
▷ Concept of loop – repeat a portion of
pseudocode/code a finite number of times

▷ Concept of array – store a group of elements


together and use common name and index or
Flow chart to find the sum of numbers
from 1 to N-1
Algorithm to add n numbers
Input: Numbers
Output: Sum of the numbers

Start

Let sum = 0

For i = 1 up to n repeat
Get the number
Let sum = sum +

Print the answer sum

Stop
Algorithm to find greatest of n numbers
Input: Numbers
Output: Greatest of the numbers

Start

Let g =

For i = 2 up to n repeat
If > g then let g =

Print the answer g

Stop
Algorithm to check prime or not
Input: Given positive number n greater than 2
Output: Whether n is prime or not
▷ Start
▷ Get number n
▷ Let p = 1
▷ For i = 2 up to n/2 repeat
▷ if (n mod i) == 0 let p = 0
▷ If p == 1 print prime else print not prime
▷ Stop
Algorithm to find factorial
Input: A positive number n
Output: Factorial of n, n!
▷ Start
▷ Let fact = 1
▷ For i = 2 up to n repeat
fact = fact * i
▷ Print fact
▷ Stop
Fibonacci Series

▷ 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

You might also like