Problem Solving and Algorithms Guide
Problem Solving and Algorithms Guide
&
Algorithms
-Jagannath Kumar Ch
Introduction to problem solving
Problem solving in computer science is the process of defining a problem,
developing a plan or approach to solve it, and then implementing that plan using
programming, algorithms, or other computational methods.
It involves breaking down a complex task into smaller, more manageable steps,
applying logical thinking and creativity to find efficient solutions.
Here are the main steps involved in problem-solving in computer science
Understanding the Problem:
o The first step is to clearly understand what the problem is asking. This means reading and
analyzing the problem carefully.
Designing a Solution:
o Once you understand the problem, you think of possible ways to solve it. This could involve
using algorithms, data structures, or patterns you already know.
Implementing the Solution:
o After deciding on an approach, you write the code to solve the problem. This is where
programming languages like Python, Java, or C++ come into play.
Testing and Debugging:
o After implementing the solution, you test it with different inputs to make sure it works. If
there are errors, you debug the code until it works correctly.
Optimizing the Solution:
o After you get the solution working, you may try to make it faster, more efficient, or more
memory-friendly.
-Jagannath Kumar Ch
Introduction to an Algorithm
Algorithms are the fundamental building blocks of computer programs. Just as recipes are
essential in cooking, algorithms are crucial for programming.
An algorithm is a well-defined step-by-step procedure that takes an input, processes it,
and produces output.
In simple term, it’s a set of instructions designed to perform a task or solve a problem.
The study of algorithms is one of the key foundations of computer science
Understanding algorithms helps in solving everyday problems and devising strategies to
handle various situations efficiently
Examples of Algorithms in Daily Life:
❖ Watching TV ( How to operate remote in front of TV)
❖ Making a Cup of Coffee or Tea (step by step process to making a coffee)
❖ Planning a Picnic (Sharing money and planning a picnic)
❖ Daily Activities ( Everyday Tasks )
-Jagannath Kumar Ch
Expressing Algorithms
➢ Algorithms can be expressed in various ways, depending on the level of
abstraction, the intended audience, and the context in which the algorithm will
be implemented or understood.
➢ The most common ways to express algorithms.
-Jagannath Kumar Ch
What is an Algorithm
➢ An Algorithm is a well-defined, finite sequence of steps or operations that
provides a solution to a specific problem.
➢ It must guarantee a solution in a finite number of steps and must be clear,
unambiguous, and effective.
➢ Algorithms are used to transform input data into desired output.
➢ In simple terms, an algorithm is a set of instructions that defines a procedure
for solving a particular problem.
➢ Key Points of an algorithm
o The algorithm should always lead to the desired output.
o It must stop after a finite number of steps, otherwise, it is not considered as an
algorithm.
o Each step must be clearly defined and unambiguous.
➢ Algorithms are fundamental to problem-solving in computer science
➢ When designing an algorithm, it’s important to ensure that it’s clear, finite,
effective, and produces the correct output for any given input.
-Jagannath Kumar Ch
Think Before Writing An Algorithms - Why?
➢ Before writing an algorithm, it is important to understand the problem or function
that needs to be performed very clearly.
➢ If you are not sure how to perform the task, you will not be able to write a correct
or efficient algorithm.
➢ Once you have a clear understanding of the task, you should state the steps
precisely and logically.
➢ You must also think about the input specification (what data will be given) and the
output specification (what result is expected).
➢ These are the essential ingredients of an algorithm:
o Input specification – the data that is to be provided.
o Output specification – the result that is to be obtained.
o Without these ingredients, the algorithm will be incomplete.
➢ An algorithm is the heart of problem solving in both Computer Science and real
life.
➢ It helps us to plan our approach step by step before converting it into a computer
program.
-Jagannath Kumar Ch
Example : Finding the Sum of Two Numbers
Problem: Write an algorithm to find the sum of two numbers.
Step 1: Input Specifications : Two numbers (say, A and B).
Step 2: Output Specifications : The sum of A and B. (say, SUM).
Step 3: Procedure of Algorithm:
3.1 Start
3.2 Read two numbers A and B
3.3 Compute SUM = A + B
3.4 Display SUM
3.5 Stop
Informal Definition of an Algorithm
(Used in a Computer)
➢ Before writing an algorithm for solving a problem, we should clearly identify the following:
o Input(s): What data or values will be given to the algorithm.
o Output(s): What result, or information is expected after executing the algorithm. Input
➢ An algorithm is a step-by-step procedure or a set of instructions used to solve a particular problem.
Symbol Meaning Example Explanation
+ Addition A+B Adds A and B Algorithm
- Subtraction A-B Subtracts B from A
* Multiplication A*B Multiplies A and B
A Step-by-step method for solving
/ Division A/B Divides A by B a problem or doing a task
Output
>= Greater than or equal to A >= B True if A is greater than or equal to B
-Jagannath Kumar Ch
Disadvantages of Algorithm
➢ Time-Consuming to Develop
o Designing an algorithm is often time-consuming and laborious.
o After developing an algorithm, it still needs to be converted into a flowchart and
then into a program
➢ Not Suitable for Complex Problems
o For very complex or large problems, writing an algorithm can become
difficult to manage and understand.
➢ Requires Prior Knowledge and Skill
o To design an efficient algorithm, one must have good analytical and logical
skills; otherwise, the algorithm may not be optimal.
-Jagannath Kumar Ch
Algorithm Efficiency
➢ Algorithm efficiency refers to how effectively an algorithm uses time and
memory resources while solving a problem.
➢ It helps us determine which algorithm performs better when there are multiple
ways to solve the same problem.
➢ There are two main measures of algorithm efficiency
❖ Time Efficiency (Speed)
o Time efficiency refers to how long an algorithm takes to complete its execution.
o An algorithm that executes in the least amount of time is considered more efficient in terms
of speed
❖ Space Efficiency (Memory Usage)
o Space efficiency refers to how much computer memory (RAM) an algorithm uses during its
execution.
o An algorithm that uses less memory is more efficient in terms of space
-Jagannath Kumar Ch
Algorithm to Convert Temperature from Fahrenheit to Celsius
➢ Expected output: F = 50
-Jagannath Kumar Ch
Types of Control Structures in Algorithms
➢ In programming and algorithm design, control structures determine the order in which
instructions are executed.
➢ There are three basic types of control structures:
❖ Sequence
❖ Selection (Decision | Branching)
❖ Repetition (Looping)
Type of Example
Purpose Description
Structure Keyword
Sequence Executes steps in order — Straight-line execution
Selection Chooses between alternatives if / else Executes based on a condition
Executes repeatedly until a
Repetition Repeats a set of steps for / while
condition fails
-Jagannath Kumar Ch
Sequence Algorithm
➢ A sequence algorithm is an algorithm in which a set of instructions are executed in a
specific linear order, one after another.
➢ Each instruction is carried out exactly once, and the result of one step may be used
in the next step.
➢ In other words, a sequence algorithm follows a top-to-bottom flow of execution
without any branching (decision) or looping (repetition).
➢ Example :- Algorithm to Find the Average of Two numbers?
Step1: Start
Step2: Declare the variables num1 ,num2, sum, avg
Step3: Set sum 0, avg 0
Step4: Read | Input the values are num1 ,num2
Step5: Set sum num1 + num2
Step6: Set avg sum/2
Step7: Display “The Average of two numbers :”, avg
Step8: Stop
➢ Note :-
❖ The algorithms you have studied earlier (like sum, average, area, etc.) are examples of
sequence algorithms.
❖ In flowcharts, each statement in a sequence algorithm is placed inside a rectangular process
box, and arrows show the direction of flow from one step to the next.
-Jagannath Kumar Ch
Selection (Decision | Branching) Algorithm
➢ In programming and algorithm design, selection, or decision making, or
branching refers to choosing one course of action from two or more
alternatives based on a condition.
➢ The most common form of decision-making structure is the IF-THEN-ELSE
statement.
❖ A conditional statement tests whether a given condition is True or False.
❖ If the condition is True, one set of actions is executed.
❖ If the condition is False, a different set of actions(if any) may be executed.
-Jagannath Kumar Ch
Algorithm: To Check Voter Eligibility
➢ Inputs to the algorithm:
Input age of the person (age)
➢ Expected output:
Message showing whether the person is eligible to vote or not
➢ Algorithm
Step1: Start
Step2: Declare variable age
Step3: Read the value of age
Step4: IF age greater than or equal to 18 THEN
4.1 Display “you are eligible to vote”
ELSE
4.2 Display “you are not eligible to vote”
END IF
Step5: Stop
-Jagannath Kumar Ch
Algorithm : To Declare Pass or Fail Based on Subject
Marks
➢ Inputs to the algorithm:
Input Subject marks (sub_Mark)
➢ Expected output:
Display either “Pass” or “Fail”
➢ Algorithm
Step1: Start
Step2: Declare variable sub_Mark
Step3: Read the value of sub_Mark
Step4: IF sub_Mark greater than or equal to 50 THEN
4.1 Display “Pass”
ELSE
4.2 Display “Fail”
Step5: END IF
Step6: Stop
-Jagannath Kumar Ch
Algorithm : To Check Whether a Given Year is a
Leap Year or Common Year
➢ Inputs to the algorithm:
Input A year (Integer)
➢ Expected output:
Display whether the year is a Leap Year or Common Year
➢ Algorithm
Step1: Start
Step2: Declare variable year
Step3: Read the value of year
Step4: IF ( year % 4 equals to 0 AND year %100 not equals to 0 ) OR( year % 400 equals to 0 ) THEN
4.1 Display “Leap Year”
Input
ELSE Condition Check Output
(Year)
4.2 Display “Common Year” 2020 2020 % 4 == 0 and 2020 % 100 != 0 Leap Year
Step5: END IF 1900 1900 % 4 == 0 but 1900 % 100 == 0 (not %400) Common Year
-Jagannath Kumar Ch
Loop | Repetition algorithm Concepts
➢ A loop is a sequence of instructions that is executed repeatedly until a certain
condition is met.
➢ A complete execution of a loop is called an iteration.
➢ There are two main types of loops in most programming languages.
❖ WHILE Loop
❖ FOR Loop
➢ WHILE and FOR loops are used to execute a sequences of statements multiple
times.
➢ While Loop
❖ The statements inside a while loop are executed as long as the condition is True.
❖ The loop terminates when the condition becomes False
❖ A while loop always evaluates the condition first before executing the loop body
-Jagannath Kumar Ch
Algorithm to print numbers from 1 to 100 (while)
➢ A for loop lets you initialize a counter, check a condition, and update the counter all in one
concise line.
-Jagannath Kumar Ch
Print Even numbers from 1 to 100
➢ Inputs to the algorithm:
Input n (the upper limit, here 100)
➢ Expected output:
Display 2,4,6……100
➢ Algorithm
Step 1: Start
Step 2: Declare variables n,and i.
Step3: Input the value of n
Step4: Initialize variable i
4.1 Set i1
Step5: Repeat the following steps Until i less than or equals to n
5.1: IF i module 2 equals to 0 THEN
5.1.1 Display i
5.2 END IF
5.3 Increment i
Step6: Stop
-Jagannath Kumar Ch
Algorithm : To find the factorial of a number
➢ Inputs to the algorithm:
Input An integer n
➢ Expected output:
Factorial of n (denoted as n!)
➢ Algorithm
Step 1: Start
Step 2: Declare variables n,factorial and i
Step3: Read the value of n.
Step4: Initialize variables
4.1 Set factorial ← 1
4.2 Set i ← 1
Step 5: Repeat the following steps Until i less than or equals to n
5.1 Set factorial factorial * i
5.2 Increment i
Step 6: Display “Factorial of n! : ”,factorial
Step 7: Stop -Jagannath Kumar Ch
Algorithm: To find the factors of a given number
➢ Inputs to the algorithm:
Input An integer fact (i.e. for example 6)
➢ Expected output:
Factors of fact : - 1 2 3 6
➢ Algorithm
Step 1: Start
Step 2: Declare variables fact and i
Step3: Read the value of fact
Step4: Initialize variable
4.1 Set i←1
Step5: Repeat the following steps Until i less than or equal to fact
5.1 IF fact % i equal to 0 THEN
5.1.1 Display i // i is factor of fact
5.2 END IF
5.3 Increment i
Step 6: End
-Jagannath Kumar Ch
Nested Control Structures
➢ In programming, nested control structures refer to using one control
structure within another.
➢ This means placing a decision-making or looping statement inside another
control statement
➢ Such combinations include
➢ A loop within another loop (nested loops)
➢ An if statement within another if (nested if)
➢ An if statement inside a loop
➢ A loop inside an if statement, and so on
➢ It is quite common to use nested control structures when solving complex
problems that require multiple levels of decision-making or repetition.
➢ However, as the level of nesting increases, the logic may become more
difficult to read and understand.
➢ In such cases, it is a good practice to divide the algorithm into smaller, well-
defined modules or functions for better clarity and maintenance.
-Jagannath Kumar Ch
Algorithm: To find the largest among three different numbers
Inputs to the algorithm:
Three numbers a, b and c
Expected output:
The largest number among a, b and c
Algorithm
Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read the values of a, b and c.
Step 4: IF a greater than b THEN
4.1 IF a greater than c THEN
4.1.1 Display a,” is the largest number”.
ELSE
4.1.2 Display c ,“is the largest number”.
4.2 END IF
ELSE
4.3 IF b greater than c THEN
4.3.1 Display b ,“is the largest number”
ELSE
4.3.2 Display c, “ is the greatest number”
4.4 END IF
Step 5: END IF
Step 6: Stop
-Jagannath Kumar Ch
Multiplication Tables from 1 to n up to m length for each
Inputs to the algorithm:
n - Number of tables to be displayed
m - Length of each table
Expected output:
Tables from 1 to n, each up to m.
Algorithm
[Link]
[Link] variable i, j, n, m
[Link] the value of n ( number of tables)
[Link] the value of m( length of each table)
[Link] i 1
[Link] i less than or equals to n
6.1 Set j 1
6.2 WHILE j less than or equals to m
6.2.1 Print i ,"x", j, "=", i*j
6.2.2 Set j j+1
6.3 END WHILE
6.4 Set i i+1
[Link] WHILE
[Link] -Jagannath Kumar Ch
Write an algorithm to check whether a number entered by user is prime or not
Inputs to the algorithm:
An integer number n entered by the user
Expected output:
Display whether the given number is Prime or Not Prime.
Algorithm
Step 1: Start
Step 2: Declare variables n, i, factors _count.
Step 3: Read the value of n
Step 4: Initialize variables
4.1 Set factors _count 0
4.2 Set i 1
Step 5: Repeat the steps Until i less than or equals to n
5.1 IF n module i equals to 0 THEN
5.1.1 Set factors _count ← factors _count +1
5.2 END IF
5.3 Increment i
Step 6: IF factors _count equals to 2 THEN
6.1 Display “ The given number ”+ n +” is Prime”
ELSE
6.2 Display “ The given number ”+ n +” is Not Prime”
Step 7: END IF
Step 8: Stop
-Jagannath Kumar Ch
Write an algorithm to find the Fibonacci series till term≤100.
Inputs to the algorithm:
No specific input required (the limit value is fixed as 100)
Expected output:
Display 0 1 1 2 3 5 8 …….89
Algorithm
Step 1: Start
Step 2: Declare variables f1, f2 and next.
Step 3: Input “Enter Fibonacci Series n value :”, n_range
Step 4: Initialize the first two Fibonacci numbers
4.1 Set f1 ← 0
4.2 Set f2 ← 1
Step 5: Repeat the steps Until f less than or equals to n_range
5.1: Display f1
5.2: Set next ← f1 + f2
5.3: Set f1 ← f2
5.4: Set f2 next
Step 6: Stop
-Jagannath Kumar Ch
what is pseudocode ?
➢ In computer science, pseudocode is a simplified, informal way of
describing a program’s logic or algorithm using plain language mixed with
basic programming structures (like loops, conditionals, and functions).
➢ It’s not actual code, It can’t be run on a computer
➢ But it helps programmers plan and communicate ideas before writing real
code in a specific programming language (like Python, C++, or Java).
➢ Basic Rules : There’s no strict syntax, but pseudocode usually
❖ Uses simple English to describe actions
❖ Uses control structures like
o IF ... THEN ... ELSE
o FOR, WHILE
o INPUT, OUTPUT
o BEGIN / END
-Jagannath Kumar Ch
Find the Largest of Two Numbers Calculate the Sum of the First N Numbers
BEGIN BEGIN
END END
Check if a Number is Even or Odd Print Numbers from 1 to 10 Using a WHILE Loop
BEGIN BEGIN
ENDIF END
END
-Jagannath Kumar Ch