0% found this document useful (0 votes)
20 views13 pages

NEP FY Unit 2

The document provides an overview of algorithms, defining them as step-by-step procedures for problem-solving, and categorizing them into types such as recursive, divide and conquer, dynamic programming, greedy, brute force, and backtracking algorithms. It outlines the characteristics and terminology of algorithms, including input, output, conditions, and loops, along with advantages and disadvantages. Additionally, it includes examples of algorithms and flow charts to illustrate the concepts discussed.

Uploaded by

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

NEP FY Unit 2

The document provides an overview of algorithms, defining them as step-by-step procedures for problem-solving, and categorizing them into types such as recursive, divide and conquer, dynamic programming, greedy, brute force, and backtracking algorithms. It outlines the characteristics and terminology of algorithms, including input, output, conditions, and loops, along with advantages and disadvantages. Additionally, it includes examples of algorithms and flow charts to illustrate the concepts discussed.

Uploaded by

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

US1MACSC01: Computer Fundamentals - I

Semester – I
Unit – 2: Problem Solving Through Logic Development - I

Concept of an Algorithm:

Definition: An algorithm is a well-defined step by step procedure to solve a particular task


(problem). It is also known as logic. It is written in simple English like language.

An algorithm includes calculations, reasoning, and data processing. Algorithms can be


presented by natural languages, pseudocode, and flowcharts, etc.

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.

 Divide and Conquer Algorithm

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.

 Dynamic Programming Algorithm

Developed by Richard Bellman in the 1950s, the dynamic programming algorithm is


generally used for optimization problems. In this type of algorithm, past results are
collected for future use. Like the divide and conquer algorithm, a dynamic programming
algorithm simplifies a complex problem by breaking it down into some simple sub-
problems.

 Greedy Algorithm

This is another way of solving optimization problems – greedy algorithm. It refers to


always finding the best solution in every step instead of considering the overall
optimality. That is to say, what he has done is just at a local optimum.

US1MACSC01 UNIT-2 Page 1 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

 Brute Force 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

Based on a depth-first recursive search, the backtracking algorithm focusing on finding


the solution to the problem during the enumeration-like searching process. When it
cannot satisfy the condition, it will return “backtracking” and tries another path. It is
suitable for solving large and complicated problems, which gains the reputation of the
“general solution method”. One of the most famous backtracking algorithm example it
the eight queens puzzle.
Characteristics of Algorithms:

 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.

 Definiteness: An algorithm must have a well-defined set of steps or instructions that


are executed in a specific order. These steps must be clear and unambiguous.

 Finiteness: The algorithm must terminate after a finite number of steps. It must not run
indefinitely or get stuck in an infinite loop.

 Feasibility: An algorithm must be implementable, meaning that it can be translated


into a program that can be executed on a computer.

 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

In summary, an algorithm is a set of well-defined, finite, and effective steps or


instructions for solving a problem or processing data, which must have inputs and
outputs, be feasible and implementable, and have a level of generality and optimality.

Terminology used for Writing ALGORITHM:


 START and STOP
 INPUT and OUTPUT
 CONDITION
 LOOP

START and STOP:


 Every algorithm must begin with a START statement.
 START indicates the starting of an algorithm.
 Every algorithm must be ends with a STOP statement.
 STOP indicates the ending of an algorithm.

INPUT and OUTPUT:

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’

US1MACSC01 UNIT-2 Page 3 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

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.

E.g. Write an algorithm to print 1 to 5 numbers using REPEAT … UNTIL


Step 1. START
Step 2. I = 1
Step 3. REPEAT step 4 TO step 5 UNTIL I <= 5
Step 4. WRITE I
Step 5. I = I + 1
Step 6. STOP

E.g. Write an algorithm to print series the 1 2 3 4 5 . . . up to N using REPEAT …


UNTIL
Step 1. START
Step 2. READ N
Step 3. I = 1
Step 4. REPEAT step 5 TO step 6 UNTIL I <= N
Step 5. WRITE I
Step 6. I = I + 1
Step 7. STOP

Example:

1: Write an algorithm to add two numbers.

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

US1MACSC01 UNIT-2 Page 4 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

Advantages of Algorithm:

 It is easy to understand and easy to write.


 It is an effective method of solving certain sets of problems
 No need to knowledge of a specific programming language
 It is easy to detect and solved the mistake from the algorithm.

Disadvantages of Algorithm:

 It is time consuming process.


 There is only a manual method to check whether an algorithm is correct or
not.

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

[4]. Write an algorithm to print factorial for the given number N.


Input: N = 4
Output: 24 (Hint: N! = 1*2*3*4)

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

US1MACSC01 UNIT-2 Page 6 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

Flow Chart:

 A pictorial representation of a system prepared on the basis of symbols and relational


lines. The specific type of flow chart is prepared for indicating the input, processing
and output from each run.
 Program flow charts are often prepared as a preliminary to program coding to
determine the logical aspects of the specific problem and the sequence of steps to be
incorporated in the program. There are number of standard symbols used in flow
chart.

1. Start – Stop (Terminal) Box:

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.

3. Process ( Assignment) Box:

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

Appropriate answer from diamond box.

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.

Need or Purpose of Flow Chart:

 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.

US1MACSC01 UNIT-2 Page 8 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

Example:

1. Draw a flow chart to add two numbers.


START

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

US1MACSC01 UNIT-2 Page 9 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

3: Draw a flow chart to print the series on N natural numbers.

START

READ N

I=1

N
I <=N

WRITE I

A IS
LARGER
I=I+1 STOP

Advantages of flowchart:

 It is easy to understand and easy to draw.


 It is mostly used to help the programmers in developing the program logic.
 It is a key to correct programming.
 It is an important tool in planning and designing a new system.
 It provides an overview of the system and also indicates the relationship
between the various steps.

Disadvantages of flowchart:

 It becomes difficult to draw a flowchart for the complex problem.


 Sometime, it is difficult to understand the flowchart because different users may
use different symbols.

More Example :

Examples :

[1]. Draw a flowchart to print Fibonacci series for integer N.

Input : N = 7

Output : 1 1 2 3 5 8 13

US1MACSC01 UNIT-2 Page 10 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

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

Difference between Algorithm and Flowchart:

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.

US1MACSC01 UNIT-2 Page 11 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

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

2. A flowchart is a ________ representation of an algorithm using various symbols,


shapes, and arrows to explain a process or program.
(a) Graphical (b) Visual (c) Both (a) and (b) (d) None of these

3. Why do we use a flowchart?


(a) To have a better understanding of how a process works.
(b) To evaluate a process in order to improve it.
(c) To explain how a process works to others.
(d) All of the above

4. _________ symbol used when the flow chart is starting or ending.


(a) Connector/ Arrow
(b) Terminal Box
(c) Input / Output
(d) Process

5. ___________ symbol used to connect relationships between the shapes.


(a) Connector
(b) Terminal Box
(c) Input/ Output
(d) Process

6. ___________ symbol used to calculate any data in the flowchart.


(a) Connector (b) Terminal Box (c) Input/ Output Box (d) Process Box

7. The process of drawing a flow for an algorithm is called __________


(a) Performance (b) Evaluation (c) Algorithmic Representation (d) Flowcharting

8. ______ box represent two different conditions.


(a) Rectangle (b) Diamond (c) Circle (d) Parallelogram

9. What are the three algorithms constructs?


(a) Sequence, selection, repetition (b) Input, output, process
(c) Input/output, decision, terminator (d) Loop, input/output, process

10. Why is a flowchart required?


(a) It is easy to use (b) To analyze the program
(c) To execute a program (d) To plan a program before making it.

US1MACSC01 UNIT-2 Page 12 of 13


US1MACSC01: Computer Fundamentals - I
Semester – I
Unit – 2: Problem Solving Through Logic Development - I

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

1. What is algorithm? Explain different types of the algorithm.


2. Explain characteristics of algorithm.
3. Discuss various terminologies used in writing the algorithm.
4. What do you mean by algorithm? How can we specify conditions and loops in
algorithm? Also discuss advantages and disadvantages of the algorithm.
5. Explain flowchart with its symbols.
6. What is flowchart? Why we need it? Write advantages and disadvantages of it.
7. Write an algorithm to find out whether the given number is palindrome number or not.
8. Write an algorithm to check whether the entered number is prime number or not.
9. Write an algorithm to find largest number among the two numbers. Also draw the
flowchart for the same.
10. Draw the flowchart to print Fibonacci series up to N number.

US1MACSC01 UNIT-2 Page 13 of 13

You might also like