0% found this document useful (0 votes)
20 views

python class test 2 with two mark answer

Uploaded by

Vidhya Gopinath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

python class test 2 with two mark answer

Uploaded by

Vidhya Gopinath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

QUESTION BANK
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING

UNIT – 1 - COMPUTATIONAL THINKING AND PROBLEM SOLVING


PART A
1 Write an algorithm to find smallest among three numbers. ( April/may 2022)
Step 1: Start
Step 2: Read the three numbers A, B, C Step 3: Compare A and B.
If A is minimum, go to step 4 else go to step 5.
Step 4: Compare A and C.
If A is minimum, output “A is minimum” else output “C is minimum”. Go to step 6.
Step 5: Compare B and C.
If B is minimum, output “B is minimum” else output “C is minimum”.
Step 6: Stop
2 Write an algorithm to accept two numbers, perform the sum and print the result.( Jan 2022)
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop
3 List the symbol used in drawing the flowchart ( April/may 2019)

4 Distinguish between algorithm and program. (DEC/JAN 2019)


An algorithm is a systematic logical approach used to solve problems in a
computer while pseudo code is the statement in plain English that may be
translated later to a programming language. Pseudo code is the intermediary
between algorithm and program.
5 Write an algorithm to find the minimum number in the given list of number. (DEC/JAN
2019)
1. Assign the first value of the list as minimum
2. Compare this value to the other values starting from second value
3. When a value is smaller than the present minimum value, then it becomes the new
minimum.
4. Continue the process recursively
5. Print the minimum value.
6 What is an Algorithm
Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for
completing a task. It is an English-like representation of the logic which is used to
solve
the problem. It is a step- by-step procedure for solving a task or a problem. The steps
must be ordered, unambiguous and finite in number.
7 Write down the characteristics of algorithm.
 Algorithm should be precise and unambiguous.
 Instruction in an algorithm should not be repeated infinitely.
 Ensure that the algorithm will ultimately terminate.
 Algorithm should be written in sequence.
 Algorithm should be written in normal English.
 Desired result should be obtained only after the algorithm terminates.
8 Write an pseudo-code to two accept to numbers, add the numbers and print the result (Jan
17)
Begin.
WRITE “Please enter two numbers to add”
READ num1.
READ num2.
Sum = num1+num2.
WRITE Sum.
End.
9 How will you analyze the efficiency of algorithm ? (Nov/Dec 2019)
Suppose X is an algorithm and n is the size of input data, the time and space used by the
algorithm X are the two main factors, which decide the efficiency of X
 Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
 Space Factor − Space is measured by counting the maximum memory space required
by the algorithm.

10 What is the use of algorithm ? Flowchart and Pseudo code in the perspective of problem
solving (Nov/Dec 2019)
i. Simple to understand
ii. Easy to debug
iii. Independent of programming language
iv. Each step can be easy to written in high-level language.
11 What are the factors used to judge the quality of an algorithm ? (Nov/Dec 2020)
I. Accuracy
II. Memory
III. Time
IV. Sequence
V. Result
12 What is control flow? List and define the ways of execution of control. (Nov/Dec 2020)
Control flow (or flow of control) is the order in which individual statements, instructions or
function calls of an imperative program are executed or evaluated. A control flow statement
is a statement in which execution results in a choice being made as to which of two or
more paths to follow.
i. Sequential control flow
ii. Selection control flow
iii. Iterative control flow
13 List the building blocks of algorithm?
The building blocks of an algorithm are
 Statements
 Sequence
 Selection or Conditional
 Repetition or Control flow
 Functions
14. Give the rules for writing Pseudo codes.
 Write one statement per line.
 Capitalize initial keywords.
 Indent to show hierarchy.
 End multiline structure.
 Keep statements to be language independent.
15 Give the difference between flowchart and pseudo code.
Flowchart and Pseudo code are used to document and represent the algorithm. In other
words, an algorithm can be represented using a flowchart or a pseudo code. Flowchart
is a graphical representation of the algorithm. Pseudo code is a readable, formally styled
English like language representation of the algorithm.

16 Define Recursion?
Function calls itself until the base condition is
reached.
Only base condition (terminating condition) is
Specified
It takes more memory than iteration due to
overhead of maintaining stack.

17 What is Iteration
Repetition of process until the condition fails
It involves four steps: initialization, condition,
execution and updation
Iterative approach makes our code longer
Iteration is faster.
Iteration takes less memory
18 Write down the steps involved in algorithmic problem?
Algorithm development process consists of five major steps.
Step 1: Obtain a description of the problem. Step 2: Analyze the problem.
Step 3: Develop a high-level algorithm.
Step 4: Refine the algorithm by adding more detail. Step 5: Review the
algorithm.

19 What is counter-controlled loop?


A count-controlled loop is used when the number of iterations to occur is already known. A
count-controlled loop is so called because it uses a counter to keep track of how many times
the algorithm has iterated.
Consider this simple algorithm for adding up five inputted numbers:
1. set the total to 0
2. repeat this section five times
o input a number
o add the number to the total
3. go back to step 2
4. say what the total is

20 What is Sentinel-controlled loop?


A sentinel controlled loop is also called an indefinite repetition loop because the number of
iterations is not known before the loop starts executing. In a sentinel controlled loop, a
special value called sentinel value is used to change the loop control expression from true
to false in order to determine whether to execute the loop body. Sentinel controlled loop is
useful when we don’t know in advance how many times the loop will be executed
21 Write algorithm to find odd or even number?
ALGORITHM:
Step 1: Start
Step 2: Declare variable c of integer type
Step 3: Set c=0
Step 4: Repeat step 4.1 to 4.3 while (c<=100)
Step 4.1: if (c%2 != 0)
Step 4.2: then print c
Step 4.3 : c=c+1
Step 5: Stop

22 Write pseudocode to find given number is +ve or -ve?


BEGIN

NUMBER num

OUTPUT "Enter a Number"


OKU num

IF num>0 THEN
OUTPUT "Entered number is positive"
ELSE IF num <0 THEN
OUTPUT "Entered number is negative"
ELSE
OUTPUT "Entered number is zero"
ENDIF

END

23 Draw flowchart to find area of circle?

24 What are three problem solving techniques?


Problem solving can be expressed in the form of
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. programs
Algorithm sequence of instructions that describe a method for solving a problem. In other
words it is a step by step procedure for solving a problem.

25 Write the advantages of algorithm and pseudocode?


Designing an algorithm in pseudo-code has advantages because:
 it can be quickly and easily converted into an actual programming language as it is similar
to a programming language
 it is fairly easy to understand, even for non-programmers
 it does not matter if there are errors in the syntax - it is usually still obvious what is
intended
 changes to the design can be incorporated quite easily
Pseudo-code also has its disadvantages:
 It can be hard to see how a program flows. For example, where does following one path as
opposed to another take the program?
 It can be time consuming to produce.

26 What are the limitations of flowchart?


 Complicated Logic: Program logic can be extremely complex at times. In this
instance, the flowchart becomes awkward and complicated. Moreover, Flowcharts
tend to oversimplify a process in certain situations.
 Alterations and Modifications: If changes are needed, the flowchart may need to
fully redrawn. Furthermore, This takes a lot of time and work.
 Flowchart replication: Because flowchart symbols cannot typed, flowchart
reproduction becomes difficult.
 It’s easy to get caught up in the technical intricacies of how something is done and
lose sight of what’s important.
 Sequence-altering factors are not mention.
The following are some of the drawbacks of flowcharts:
 Firstly, Complex programmes and tasks are difficult to present.
 Secondly, There is no room for change or modification.
 The issue of reproduction arises.
 It is a lengthy procedure.
 People who are unfamiliar with flowchart symbols may find it difficult to
comprehend.
 There is no man-to-machine communication.

27 List the key steps involved for solving a problem?


Step 1: Identify the Problem. As obvious as it may sound, the first step in the problem-
solving process is to identify the root of the issue. ...
Step 2: Generate Potential Solutions. ...
Step 3: Choose One Solution. ...
Step 4: Implement the Solution You've Chosen. ...
Step 5: Evaluate Results.

28 Difference between flowchart and pseudocode?


29 Write down the algorithm for finding minimum number in a list.
Algorithm: 1. Start.
2. Read the list of elements.
3. Set first element in the list as minimum.
4. Move to the next element in the list.
5. If current element< minimum then
Set minimum =current element.
6. Repeat Step 4 and Step 5 until the end of the list is reached.
7. Print the minimum value in the list
8. Stop
30 Write pseudocode for tower of hanoi.
START
Procedure Hanoi(disk, source, dest, aux)

IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF

END Procedure
STOP

PART-B

1 i. Define flow chart? Draw flowchart and write an algorithm to accept three
distinct numbers and find the greatest, print the result (Nov/Dec 2022)
A flowchart is a diagrammatic representation of the logic for solving a task. A
flowchart is drawn using boxes of different shapes with lines connecting them
to show the flow of control. The purpose of drawing a flowchart is to make the
logic of the program clearer in a visual form.

FLOECHART:
ALGORITHM:
Step 1: Start
Step 2: Read the three numbers A, B, C Step 3: Compare A and B.
If A is minimum, go to step 4 else go to step 5.
Step 4: Compare A and C.
If A is minimum, output “A is minimum” else output “C is minimum”. Go to step 6.
Step 5: Compare B and C.
If B is minimum, output “B is minimum” else output “C is minimum”.
Step 6: Stop
ii. Outline the algorithm to display the first n odd numbers(Apr/May 2019)
2 I. Draw a flowchart AND algorithm to find the sum of the series 1+2+3…..
+100 (Nov/Dec 2022)
II.What is an algorithm? Summarize the characteristics of a good algorithm.
(Apr/May 2019)
I. FLOWCHART
ALGORITHM:
Read n value as 100.
declare k variable.
Uss formula n*(n+1)/2 to get sum of first 100 natural numbers and hold it in the
variable k.
print k.

II. ALGORITHM:

Algorithm is an ordered sequence of finite, well defined, unambiguous


instructions for completing a task. It is an English-like representation of the
logic which is used to solve
the problem. It is a step- by-step procedure for solving a task or a problem. The
steps must be ordered, unambiguous and finite in number.

CHARECTERISTICS:
 Algorithm should be precise and unambiguous.
 Instruction in an algorithm should not be repeated infinitely.
 Ensure that the algorithm will ultimately terminate.
 Algorithm should be written in sequence.
 Algorithm should be written in normal English.
 Desired result should be obtained only after the algorithm terminates.

3 What is a programming language ? What are its types ? Explain in detail with
their advantages and disadvantages (Nov/Dec 2019)
• A programming language is a set of symbols and rules for instructing a computer
to perform specific tasks.
• The programmers have to follow all the specified rules before writing program
using programming language.
• The user has to communicate with the computer using language which it can
understand

Types of programming language

1. Machine language
2. Assembly language
3. High level language
MACHINE LANGUAGE:
The computer can understand only 0’s and 1’s.
• In machine language the different instructions are formed by taking different
combinations of 0’s and 1’s.
Advantages:
Translation free:
• For executing any program written in any programming language, the conversion
to machine language is necessary.
• The conversion process is not required.
High speed
• The machine language program is translation free.
• Conversion time is saved
the execution of machine language program is extremely fast
Disadvantage:
• It is hard to find errors in a program written in the machine language.
• Writhing program in machine language is a time consuming process.
Machine dependent:
• According to architecture used, the computer differs from each other.
• Machine language differs from computer to computer.
• Program developed for a particular type of computer may not run on other type
of computer.
ASSEMBLY LANGUAGE:
• To overcome the issues in programming language and make the programming
process easier, an assembly language is developed which is logically
equivalent to machine language but it is easier for people to read, write and
understand.
• Assembly languages are symbolic programming language that uses symbolic
notation to represent machine language instructions.
• They are called low level language because they are so closely related to the
machines.
ASSEMBLER
Assembler is the program which translates assembly language instruction in to a
machine language.
• Easy to understand and use.
• It is easy to locate and correct errors.

Disadvantage
1. Machine dependent
The assembly language program which can be executed on the machine depends
on the architecture of that computer.
2. Hard to learn
It is machine dependent, so the programmer should have the hardware
knowledge to create applications using assembly language.
3. Less efficient
Execution time of assembly language program is more than machine language
program.
Because assembler is needed to convert from assembly language to machine
language.
HIGH LEVEL LANGUAGE
High level language contains English words and symbols.
The specified rules are to be followed while writing program in high level
language.
The interpreter or compilers are used for converting these programs in to
machine readable form.
Translating high level language to machine language
Compiler:
• A compiler is a program which translates the source code written in a high level
language in to object code which is in machine language program.
• Compiler reads the whole program written in high level language and translates
it to machine language.
• If any error is found it display error message on the screen.
Interpreter
• Interpreter translates the high level language program in line by line manner.
• The interpreter translates a high level language statement in a source program to
a machine code and executes it immediately before translating the next
statement.
• When an error is found the execution of the program is halted and error message
is displayed on the screen.
Advantages
Readability
• High level language is closer to natural language so they are easier to learn and
understand
Machine independent
• High level language program have the advantage of being portable between
machines.
Easy debugging
• Easy to find and correct error in high level language
Disadvantages
Less efficient
• The translation process increases the execution time of the program. Programs
in high level language require more memory and take more execution time to
execute.
They are divided into following categories:
Interpreted programming languages
• Functional programming languages
• Compiled programming languages
• Procedural programming languages
• Scripting programming language
• Markup programming language
• Concurrent programming language
• Object oriented programming language
4 Outline the algorithm, flowchart and pseudocode to display the first n odd
numbers(Apr/May 2019)
ALGORITHM:
Step 1: Start

Step 2: Declare variable c of integer type

Step 3: Set c=0

Step 4: Repeat step 4.1 to 4.3 while (c<=100)

Step 4.1: if (c%2 != 0)

Step 4.2: then print c

Step 4.3 : c=c+1

Step 5: Stop

FLOWCHART:

PSEUDOCODE:
1. INPUT n.
2. remainder = n % 2.
3. IF remainder is not equal to 0.
4. answer = odd.
5. ELSE.
6. answer = even.
7. OUTPUT answer.

5 Discuss the building blocks of an algorithm(Nov/Dec 2019)


BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
Algorithms can be constructed from basic building blocks namely, sequence, selection
and iteration.
Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following actions
Ø input data-information given to the program
Ø process data-perform operation on a given input
Ø output data-processed result
State:
Transition from one process to another process under specified condition with in a time
is called state.
Control flow:
The process of executing the individual statements in a given order is called control
flow.
The control can be executed in three ways
1. sequence
2. selection
3. iteration
Sequence:
All the instructions are executed one after another is called sequence execution.

Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop

Selection:
A selection statement causes the program control to be transferred to a specific part of
the program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop

Iteration:
In some programs, certain set of statements are executed again and again based upon
conditional test. i.e. executed more than one time. This type of execution is called
looping or iteration.

Example

Write an algorithm to print all natural numbers up to n


Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop

Functions:

v Function is a sub program which consists of block of code(set of instructions) that


performs a particular task.
v For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.

Benefits of Using Functions

v Reduction in line of code


v code reuse
v Better readability
v Information hiding
v Easy to debug and test
v Improved maintainability
Example:

Algorithm for addition of two numbers using function


Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop

sub function add()


Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return

Def add(a,b):
Sum=a+b
Return sum
Num1=10
Num2=20
Print(‘the sum is’,add(num1,num2))
6 Draw flowchart to print the first n prime numbers(Apr/May 2017)
IS THIS NUMBER PRIME:
7 State the Tower of Hanoi problem. Outline the solution to the Tower of Hanoi problem with r
diagram. (Nov/Dec 2020)
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one
as depicted –
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller o
over the larger one. There are other variations of the puzzle where the number of disks increase,
tower count remains the same.
RULES
The mission is to move all the disks to some another tower without violating the sequence of arrang
A few rules to be followed for Tower of Hanoi are −
 Only one disk can be moved among the towers at any given time.
 Only the "top" disk can be removed.
 No large disk can sit over a small disk.Following is an animated representation of solving a

of Hanoi puzzle with three disks.


Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation show
puzzle with 3 disks has taken 23 - 1 = 7 steps.
ALGORITHM
We mark three towers with name, source, destination and aux (only to help moving the disks)
have only one disk, then it can easily be moved from source to destination peg.
If we have 2 disks −
 First, we move the smaller (top) disk to aux peg.
 Then, we move the larger (bottom) disk to destination peg.

 And finally, we move the smaller disk from aux to destination peg.
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two dis
divide the stack of disks in two parts. The largest disk (n th disk) is in one part and all other (n-1) d
in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks onto
can imagine to apply the same in a recursive way for all given set of disks.
The steps to follow are −
Step 1 − Move n-1 disks from source to aux
Step 2 − Move nth disk from source to dest
Step 3 − Move n-1 disks from aux to dest
A RECURSIVE ALGORITHM FOR TOWER OF HANOI CAN BE DRIVEN AS FOLLOW
START
Procedure Hanoi(disk, source, dest, aux)

IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP

FLOWCHART

8 Identify the simple strategies for developing an algorithm (Nov/Dec 2019)


1. iterations
2. Recursions

1. Iterations:
A sequence of statements is executed until a specified condition is true is called iterations.
1. for loop
2. While loop
Syntax for For:
FOR( start-value to end-value) DO
Statement
...
ENDFOR

Example: Print n natural numbers


BEGIN
GET n
INITIALIZE i=1
FOR (i<=n) DO
PRINT i
i=i+1
ENDFOR
END

Syntax for While:


WHILE (condition) DO
Statement
...
ENDWHILE

Example: Print n natural numbers


BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i
i=i+1
ENDWHILE
END
Recursions:
v A function that calls itself is known as recursion.
v Recursion is a process by which a function calls itself repeatedly until some specified condit
been satisfied.

Algorithm for factorial of n numbers using recursion:


Main function:
Step1: Start
Step2: Get n
Step3: call factorial(n)
Step4: print fact
Step5: Stop

Sub function factorial(n):


Step1: if(n==1) then fact=1 return fact
Step2: else fact=n*factorial(n-1) and return fact
Pseudo code for factorial using recursion:
Main function:
BEGIN
GET n
CALL factorial(n)
PRINT fact
BIN
Sub function factorial(n):
IF(n==1) THEN
fact=1
RETURN fact
ELSE
RETURN fact=n*factorial(n-1)

9 What is Algorithmic Problem Solving and explain the key steps involved for solving a problem?
Understanding the Problem
v It is the process of finding the input of the problem that the algorithm solves.
v It is very important to specify exactly the set of inputs the algorithm needs to handle.
v A correct algorithm is not one that works most of the time, but one that works c
for all legitimate inputs.
Ascertaining the Capabilities of the Computational Device
v If the instructions are executed one after another, it is called sequential algorithm.
v If the instructions are executed concurrently, it is called parallel algorithm.
Choosing between Exact and Approximate Problem Solving
v The next principal decision is to choose between solving the problem exactly or sol
approximately.
v Based on this, the algorithms are classified as exact algorithm and approximation algorithm.
Deciding a data structure:
v Data structure plays a vital role in designing and analysis the algorithms.
v Some of the algorithm design techniques also depend on the structuring data specifying a pro
instance
v Algorithm+ Data structure=programs.
Algorithm Design Techniques
v An algorithm design technique (or “strategy” or “paradigm”) is a general approach to
problems algorithmically that is applicable to a variety of problems from different areas of computi
v Learning these techniques is of utmost importance for the following reasons.
v First, they provide guidance for designing algorithms for new problems,

Methods of Specifying an Algorithm


v Pseudocode is a mixture of a natural language and programming language-like constructs. Pseu
is usually more precise than natural language, and its usage often yields more succinct al
descriptions.
v In the earlier days of computing, the dominant vehicle for specifying algorithms was a flowc
method of expressing an algorithm by a collection of connected geometric shapes containing desc
of the algorithm’s steps.
v Programming language can be fed into an electronic computer directly. Instead, it need
converted into a computer program written in a particular computer language. We can look at
program as yet another way of specifying the algorithm, although it is preferable to consider i
algorithm’s implementation.

Proving an Algorithm’s Correctness


v Once an algorithm has been specified, you have to prove its correctness. That is, you have to pr
the algorithm yields a required result for every legitimate input in a finite amount of time.
v A common technique for proving correctness is to use mathematical induction because an algo
iterations provide a natural sequence of steps needed for such proofs.
v It might be worth mentioning that although tracing the algorithm’s performance for a few
inputs can be a very worthwhile activity, it cannot prove the algorithm’s correctness conclusively
order to show that an algorithm is incorrect, you need just one instance of its input for which the alg
fails.
Analysing an Algorithm
1. Efficiency.
Time efficiency, indicating how fast the algorithm runs,
Space efficiency, indicating how much extra memory it uses.

2. simplicity.
v An algorithm should be precisely defined and investigated with mathematical expressions.
v Simpler algorithms are easier to understand and easier to program.
v Simple algorithms usually contain fewer bugs.
Coding an Algorithm
v Most algorithms are destined to be ultimately implemented as computer programs. Programm
algorithm presents both a peril and an opportunity.
v A working program provides an additional opportunity in allowing an empirical analysis
underlying algorithm. Such an analysis is based on timing the program on several inputs an
analysing the results obtained.

4 Write algorithm, flowchat and pseudocode for guessing an integer number in a range(binary search

Algorithm:
Step1: Start
Step 2: Declare hidden, guess
Step 3: Compute hidden= Choose a random value in a range
Step 4: Read guess
Step 5: If guess=hidden, then
Print Guess is hit
Else
Print Guess not hit
Print hidden
Step 6: Stop

Pseudocode:
BEGIN
COMPUTE hidden=random value in range
READ guess
IF guess=hidden, then
PRINT Guess is hit
ELSE
PRINT Guess not hit
PRINT hidden
END IF-ELSE
END
FLOWCHART:
10 Identify the simple strategies for developing an algorithm (Nov/Dec 2019)
SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS:

1. iterations
2. Recursions

1. Iterations:

A sequence of statements is executed until a specified condition is true is called iterations.


1. for loop
2. While loop

Syntax for For:


FOR( start-value to end-value) DO
Statement
...
ENDFOR

Example: Print n natural numbers


BEGIN
GET n
INITIALIZE i=1
FOR (i<=n) DO
PRINT i
i=i+1
ENDFOR
END

Syntax for While:


WHILE (condition) DO
Statement
...
ENDWHILE

Example: Print n natural numbers


BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i
i=i+1
ENDWHILE
END
Recursions:

v A function that calls itself is known as recursion.


v Recursion is a process by which a function calls itself repeatedly until some specified condit
been satisfied.

Algorithm for factorial of n numbers using recursion:

Main function:
Step1: Start
Step2: Get n
Step3: call factorial(n)
Step4: print fact
Step5: Stop

Sub function factorial(n):


Step1: if(n==1) then fact=1 return fact
Step2: else fact=n*factorial(n-1) and return fact

Pseudo code for factorial using recursion:


Main function:
BEGIN
GET n
CALL factorial(n)
PRINT fact
BIN

Sub function factorial(n):


IF(n==1) THEN
fact=1
RETURN fact
ELSE
RETURN fact=n*factorial(n-1)
12 Write an algorithm to insert a card into a list of sorted cards(Nov/Dec 2019)

Algorithm:
Step 1: Start
Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
Step4.1: Read a[i]
Step 4.2: i=i+1 goto step 4
Step 5: Read item
Step 6: Calculate i=n-1
Step 7: If i>=0 and item<a[i], then go to step 7.1, 7.2 else goto step 8
Step 7.1: a[i+1]=a[i]
Step 7.2: i=i-1 goto step 7
Step 8: Compute a[i+1]=item
Step 9: Compute n=n+1
Step 10: If i<n, then goto step 10.1, 10.2 lse goto st 11
Step10.1: Print a[i]
Step10.2: i=i+1 goto step 10
Step 11: Stop

Pseudocode:
BEGIN
READ n
FOR i=0 to n, then
READ a[i]
INCREMENT i
END FOR
READ item
FOR i=n-1 to 0 and item<a[i], then
CALCULATE a[i+1]=a[i]
DECREMENT i
END FOR
COMPUTE a[i+1]=a[i]
COMPUTE n=n+1
FOR i=0 to n, then
PRINT a[i]
INCREMENT i
END FOR
END

Flowchart:

You might also like