NEP FY Unit 2
NEP FY Unit 2
Semester – I
Unit – 2: Problem Solving Through Logic Development - I
Concept of an Algorithm:
Types of Algorithms:
Recursive Algorithm
It refers to a way to solve problems by repeatedly breaking down the problem into sub-
problems of the same kind. The classic example of using a recursive algorithm to solve
problems is the Tower of Hanoi.
Traditionally, the divide and conquer algorithm consists of two parts: 1. breaking down a
problem into some smaller independent sub-problems of the same type; 2. finding the
final solution of the original issues after solving these more minor problems separately.
The key points of the divide and conquer algorithm are:
If you can find the repeated sub-problems and the loop substructure of the original
problem, you may quickly turn the original problem into a small, simple issue.
Try to break down the whole solution into various steps (different steps need different
solutions) to make the process easier.
Greedy Algorithm
The brute force algorithm is a simple and straightforward solution to the problem,
generally based on the description of the problem and the definition of the concept
involved. You can also use “just do it!” to describe the strategy of brute force. In short, a
brute force algorithm is considered as one of the simplest algorithms, which iterates all
possibilities and ends up with a satisfactory solution.
Backtracking Algorithm
Input: An algorithm must have zero or more inputs that define the problem to be
solved or the data to be processed. The input can be in the form of values, variables,
or any other data structures.
Output: An algorithm must have a well-defined output, which can be a single value,
multiple values, or a set of values. The output must be related to the problem that the
algorithm is trying to solve.
Finiteness: The algorithm must terminate after a finite number of steps. It must not run
indefinitely or get stuck in an infinite loop.
Effectiveness: The algorithm must be efficient and solve the problem in a reasonable
amount of time and with a reasonable amount of resources, such as memory and
computational power.
Generality: The algorithm must be able to solve a wide range of problems or process
a wide range of data, not just specific cases.
Optimality: An algorithm can be optimal if it produces the best possible solution for a
given problem, or if it produces the solution in the minimum amount of time or with the
minimum amount of resources.
US1MACSC01 UNIT-2 Page 2 of 13
US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I
INPUT:
An Input (I/P) to the algorithm is indicated by READ statement. It is used to read a value of
a variable.
E.g. suppose we want to input a number say A to the algorithm is written as READ A
E.g. suppose we want to input two numbers say A and B to the algorithm are written as
READ A, B
There are two or more variables then its name is separated by comma.
OUTPUT:
An output (O/P) from algorithm is indicated by WRITE statement. It is used to print the
value of a variable. Eg. suppose we want to display a number say C from the algorithm is
by statement WRITE C
CONDTION:
A condition statement is used when there are two possibilities of either YES (TRUE) or NO
(FALSE). A condition and their statement is represented by IF…THEN…ELSE statement.
E.g. suppose we want to find maximum number from the given two numbers say A and B.
Then its condition can be written as
IF (A > B)
THEN
WRITE ‘A is Maximum’
ELSE
WRITE ‘B is Maximum’
LOOP:
A looping statement is used when there are possibilities of performing steps for the fixed
numbers of times. A looping of a statement is represented by REPEAT…UNTIL statement.
Example:
Algorithm:
Step 1: START (Start Algorithm)
Step 2: READ A, B (Input two numbers A & B say 5 & 8)
Step 3: SUM = A + B (Add two numbers A= 5 & B= 8 and store Result is
SUM = 13)
Step 4: WRITE SUM (Print the result stored in SUM)
Step 5: STOP (End of the Algorithm)
2: Read two numbers and finds larger number and print larger number.
Algorithm:
Step 1: START
Step 2: READ A, B
Step 3: If A > B then go to STEP 6
Step 4: WRITE B IS LARGER
Step 5: GO TO STEP 7
Step 6: WRITE A IS LARGER
Step 7: STOP
Advantages of Algorithm:
Disadvantages of Algorithm:
Examples:
[1]. Write an algorithm to check whether the given number is Armstrong or not.
Input: N = 153
Output: Armstrong Number (Hint: 153 = 1*1*1 + 5*5*5 + 3*3*3 )
Step – 1 : START
Step – 2 : READ N
Step – 3 : SUM = 0, M = N
Step – 4 : REPEAT step 5 TO step 7 UNTIL N != 0
Step – 5 : D = N % 10
Step – 6 : SUM = SUM + D * D * D
Step – 7 : N = N / 10
Step – 8 : IF ( M = SUM ) THEN
WRITE ‘Given number is Armstrong’
ELSE
WRITE ‘Given number is NOT Armstrong’
Step – 9 : STOP
[2]. Write an algorithm to check whether the given number is palindrome or not.
Input : N = 12321
Output : It is palindrome number. (A number is said to be a palindrome number if its
original number and its reverse number both are same)
Step – 1 : START
Step – 2 : READ N
Step – 3 : REV = 0, M = N
Step – 4 : REPEAT step 5 TO step 7 UNTIL N != 0
Step – 5 : D = N % 10
Step – 6 : REV = REV * 10 + D
Step – 7 : N = N / 10
Step – 8 : IF( M = REV) THEN
WRITE ‘Palindrome’
ELSE
WRITE ‘Not Palindrome’
Step – 9 : STOP
US1MACSC01 UNIT-2 Page 5 of 13
US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I
[3]. Write an algorithm to check whether the given number is prime number or not.
Input: N = 11
Output: It is prime number.
(A number is said to be a prime number if it is only divisible by 1 and itself)
Step – 1 : START
Step – 2 : READ N
Step – 3 : I = 2, F = 0
Step – 4 : REPEAT step 5 TO step 6 UNTIL I < N
Step – 5 : IF ( ( N % I ) = 0 ) THEN F = 1
Step – 6 : I=I+1
Step – 7 : IF ( F=0 ) THEN
WRITE ‘Prime Number’
ELSE
WRITE ‘Not Prime Number’
Step – 8: STOP
Step – 1 : START
Step – 2 : READ N
Step – 3 : I = 1, F = 1
Step – 4 : REPEAT step 5 TO step 6 UNTIL I <= N
Step – 5 : F=F*I
Step – 6 : I=I+1
Step – 7 : WRITE F
Step – 8 : STOP
[5]. Write an algorithm to print Fibonacci series for the given number N.
Input: N = 7
Output: 1 1 2 3 5 8 13
Step – 1 : START
Step – 2 : READ N
Step – 3 : I = 1, A = 1, B = 0
Step – 4 : REPEAT step 5 TO step 9 UNTIL I <= N
Step – 5 : C=A+B
Step – 6 : A=B
Step – 7 : B=C
Step – 8 : WRITE C
Step – 9 : I=I+1
Step– 10: STOP
Flow Chart:
START STOP
A rectangle with rounded sides are used to indicate the beginning or the end of a
Procedure.
2. Input – Output Box :
READ A, B WRITE
SUM
A parallelograms are used to represent input and output operations.
SUM = A + B TEMP = A
A=B
B = TEMP
Rectangles are used to indicate any processing operations such as storage and
Arithmetic.
4. Decision Box :
Is A > B
Y
A diamond shaped boxes are used to indicate the conditions tested and exit with
US1MACSC01 UNIT-2 Page 7 of 13
US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I
5. Connector:
Frequently a flow chart becomes too long to fit in a single page. A connector is used when
a flow chart does not fit into a single page. A circle is used to represent a connector. A
letter OR digit is written inside a connector to indicate the link.
1 A
A circle is used to join different parts of a flow chart. This is called a connector. It is
necessary if a flow chart extends over more than one page.
6. Arrow Symbol :
Arrows indicate the direction to be followed in a flow chart. Every line in a flow
chart must have an arrow on it.
Flow chart is helpful in understanding the logic of complicated and lengthy problems.
Once the flow chart is drawn it becomes easy to write the program in any high level
language.
Errors can be found and solved easily by passing simple test data.
It shows the relationship between different steps in a process.
Example:
READ A, B
SUM = A + B
WRITE SUM
STOP
2. Draw a flow chart to find the larger number from given two numbers.
START
READ A, B
Y WRITE A IS
A>B
LARGER
WRITE B IS
LARGER
STOP
START
READ N
I=1
N
I <=N
WRITE I
A IS
LARGER
I=I+1 STOP
Advantages of flowchart:
Disadvantages of flowchart:
More Example :
Examples :
Input : N = 7
Output : 1 1 2 3 5 8 13
START
READ N
I=0
A=1
B=0
C=A+B
A=B
B=C
I=I+1
WRITE C
IS
Y
I <= N
?
N
STOP
Algorithm Flowchart
It is a procedure for solving problems. It is a graphic representation of a process.
The process is shown in step-by-step The process is shown in block-by-block
instruction. information diagram.
It is complex and difficult to understand. It is intuitive and easy to understand.
It is convenient to debug errors. It is hard to debug errors.
The solution is showcased in natural The solution is showcased in pictorial format.
language.
It is somewhat easier to solve complex It is hard to solve complex problem.
problem.
It costs more time to create an algorithm. It costs less time to create a flowchart.
Question Bank
1. An algorithm is a _____ that provides a series of instructions that should be carried
out in a particular order to get the desired outcome.
(a) Step-by-step process (b) flow chart process
(c) Pseudo-code process (d) None of the above
Short Questions
1. What is algorithm?
2. List out various types of algorithm.
3. What do you mean by backtracking algorithm?
4. Describe terms: Finiteness and Generality of the algorithm.
5. How can we specify condition in algorithm?
6. What are the limitations of algorithm?
7. Why we use Rectangle box in flowchart?
8. How many inflow and outflow arrows are there in decision box?
9. Explain need of flowchart?
10. What is connector?
Long Questions