0% found this document useful (0 votes)
3 views39 pages

Problem Solving and Algorithms Guide

The document provides an overview of problem solving and algorithms in computer science, detailing the steps involved in problem solving, such as understanding the problem, designing a solution, implementing it, testing, and optimizing. It explains the definition and characteristics of algorithms, their importance, and how they can be expressed in various formats. Additionally, it discusses the advantages and disadvantages of algorithms, their efficiency, and includes examples to illustrate algorithm tracing and implementation.

Uploaded by

sireeshakavi2009
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)
3 views39 pages

Problem Solving and Algorithms Guide

The document provides an overview of problem solving and algorithms in computer science, detailing the steps involved in problem solving, such as understanding the problem, designing a solution, implementing it, testing, and optimizing. It explains the definition and characteristics of algorithms, their importance, and how they can be expressed in various formats. Additionally, it discusses the advantages and disadvantages of algorithms, their efficiency, and includes examples to illustrate algorithm tracing and implementation.

Uploaded by

sireeshakavi2009
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

Problem solving

&
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 )

 A problem can be defined as an obstacle or barrier that prevents the achievement


of a desired goal or state.
 Problem-solving is the process of identifying and removing this obstacle through
a series of structured steps or activities.
-Jagannath Kumar Ch
Real Life Algorithm for Making a Cup of Coffee
➢ Step 1: Input Specifications - Before starting, clearly define the required inputs
o Inputs : 1 cup of milk, 1 teaspoon of sugar, 1 teaspoon of coffee powder, 1 teaspoon
of water.
➢ Step 2 : Coffee Preparation / Procedure
o Add 1 teaspoon of coffee powder into a cup
o Add 1 teaspoon of sugar into the same cup
o Add 1 teaspoon of water into the cup to help dissolve the coffee and sugar
o Stir the coffee powder, sugar, and water together in the cup until they are well
blended
o Boil the milk with high flame around 3 minutes
o Add the boiled milk into the cup with coffee syrup and stir the mixture
o Finally coffee ready
➢ Step 3: Output specification
o At the end of this process, you will have one cup of hot coffee that is ready to be
served.

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

➢ Natural Language : The algorithm is explained in plain, everyday language in


simple explanations for non-technical audience without worrying about syntax
➢ Pseudocode : A mixture of natural language and programming language
constructs used to represent an algorithm without worrying about specific
syntax.
➢ Flowcharts : Diagrams that represent the flow of control in an algorithm using
shapes like ovals, rectangles, and diamonds.

➢ Programming Languages (Code) : The algorithm is written in an actual


programming language (e.g., Python, Java, C++).

-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

% Modulus (Remainder) A%B Gives the remainder when A is divided by B

← Assignment A←X*3 Assigns the value of X * 3 to A

> Greater than A>B True if A is greater than B

Output
>= Greater than or equal to A >= B True if A is greater than or equal to B

< Less than A<B True if A is less than B

<= Less than or equal to A <= B True if A is less than or equal to B

== Equal to A == B True if A and B are equal

!= Not equal to A != B True if A and B are not equal


Let’s understand with a simple example.
Problem: Write an algorithm to find the product of two numbers.
Step 1: Input Specifications : Two numbers (say, A and B).
Step 2: Output Specifications : The product of A and B (say, MUL)
Step 3: Procedure of Algorithm:
3.1 Start
3.2 Read | Input two numbers A and B
3.3 Compute Set MUL  Multiplies A and B ( Ex: - A * B )
3.4 Display | Print MUL
3.5 Stop
Qualities of a good algorithm
➢ A good algorithm should possess the following qualities
➢ Well-defined Inputs and Outputs
o The inputs and outputs of the algorithm must be clearly specified.
o It should be clear what data is required as input and what result will be produced as output
➢ Clarity and Unambiguity
o Each step of the algorithm should be clear, precise, and unambiguous.
o There should be no confusion about what each instruction means.
➢ Finiteness
o The algorithm must always terminate after a finite number of steps
o It should not run indefinitely
➢ Effectiveness
o The algorithm should solve the problem in the most efficient manner (in terms of time and memory)
➢ Generality
o The algorithm should be general enough to solve all problems of a particular type, not just a single
case
➢ Independence from Programming Language
o An algorithm should be written in a language-independent way.
o It should not include specific programming code or syntax of any language.
-Jagannath Kumar Ch
Advantages of Algorithm
➢ Simple and Easy to Understand
o An algorithm is a step-by-step representation of a solution to a given problem, which
makes it easy to understand and follow.
➢ Definite Procedure
o Every algorithm has a well-defined sequence of steps that leads to a specific output for
a given input.
➢ Helps in Program Development
o It is easy to first develop an algorithm, then convert it into a flowchart, and finally
translate it into a computer program.
➢ Programming Language Independent
o An algorithm is independent of any programming language. The same algorithm can
be implemented in any language such as Python, C, or Java.
➢ Easy Debugging and Testing
o Since every step in an algorithm has its own logical meaning, it becomes easier to detect
and correct errors during program development.
➢ Optimization
o Algorithms help in optimizing a program according to available system resources like
time and memory.
➢ Improves Problem-Solving Skills
o Designing algorithms helps one think logically and analytically, improving overall
problem-solving ability.

-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

➢ Code or Program (Implementation)


o An algorithm is a step-by-step procedure or set of instructions for solving a problem.
o It can later be converted into a computer program written in a programming
language (like Python, Java, or C++)
➢ Among time and space, time efficiency (speed) is often considered the most
important factor when designing an algorithm, especially for large data sets.
-Jagannath Kumar Ch
Properties|Characteristics of an algorithm
➢ According to Donald Ervin Knuth, an algorithm must possess the following five properties
1. Finiteness 2. Definiteness 3. Input 4. Output 5. Effectiveness
➢ Finiteness
o an algorithm must terminate after a finite number of steps
o This means it should not go on forever—it must come to an end after performing a limited number of operations
➢ Definiteness
o Each step of an algorithm must be clear and unambiguous.
o That is, every instruction should have only one meaning and should be precisely defined so that it can be carried out
without confusion
➢ Input : An algorithm accepts or receives zero or more inputs
➢ Output : An algorithm produces at least one output
➢ Effectiveness
o Every step of an algorithm must be basic and feasible, meaning it can be performed exactly and in a finite amount
of time using the given inputs
➢ Structure of an algorithm : A general structure of an algorithm can be written as
Start
Step -1:…
Step -2:…
.
.
Step-n:…
Stop
-Jagannath Kumar Ch
Algorithm Tracing Technique
➢ Algorithm tracing is a technique used to manually simulate (or desk-check) the execution of
an algorithm step by step to ensure that it works correctly before running it on a computer.
➢ It helps to verify the logic, flow, and correctness of an algorithm.
➢ This method is also known as a desk check because it is usually done “on paper” without
actual program execution.

Write an algorithm to find the area of a Circle using its radius r


➢ Inputs to the algorithm:
Radius r of the Circle.
➢ Expected output:
Area of the Circle (Area)
➢ Algorithm :
Step1:Start
Step2: Read | Input the radius r of the Circle
Step3: Set Area  π*r*r ( Note: compute the area of circle using formula )
Step4: Print the value of Area
Step5: Stop
Example of Algorithm Tracing
Let’s trace the algorithm for a given value of 𝑟=5.

Step Statement / Operation Variable Values Explanation

1 Start — Begin the algorithm

2 Input radius ( r = 5 ) r=5 Read the radius

3 Assign π = 3.14 π = 3.14 Assign constant value for π

4 Compute Area ← π × r × r Area = 3.14 × 5 × 5 = 78.5 Calculate area


5 Output Area Output = 78.5 Display result

6 Stop — End the algorithm


Write an algorithm to read two numbers and find their sum

 Inputs to the algorithm: ➢ Tracing of Algorithm


Input Number 1 Given Inputs:
Input Number 2 Number1 = 5
 Expected output: Number2 = 15
Sum of Number 1 and Number 2
➢ Step-by-Step Execution (Tracing):
 Algorithm:
1. Start
Step1: Start
Step2: Set Sum  0 2. Assign sum = 0
Step3: Input the first number, Number1 3. Number1 = 5
Step4: Input the second number, Number2 4. Number2 = 15
Step5: Compute | Set Sum  Number1 + Number2
5. Compute Sum = Number1 + Number2
Step6: Display the value of Sum
Step7: Stop ❖ Sum = 5 + 15
❖ Sum = 20
6. Display : Sum
❖ 20
7. Stop

-Jagannath Kumar Ch
Algorithm to Convert Temperature from Fahrenheit to Celsius

➢ Inputs to the algorithm: ➢ Tracing of Algorithm


Input Temperature in Fahrenheit (F) Given Inputs:

➢ Expected output: F = 50

Temperature in Celsius (C) ➢ Step-by-Step Execution (Tracing):


➢ Algorithm: 1. Start
Step 1: Start 2. F = 50
Step 2: Read temperature in Fahrenheit, F 3. Compute C = (5/9) × (F − 32)
Step 3: Set C ← (5/9) × (F − 32)
❖ C = (5/9) ×(50-32)
Step 4: Print temperature in Celsius, C
❖ C = (5/9) × 18
Step 5: Stop
❖ C = 90/9
❖ C = 10
4. Display : C
❖ 10
5. Stop

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

➢ The ELSE part of the statements are optional.


➢ Note : In flowcharts, a decision is represented by the diamond-shaped box
➢ This structure is also known as a selection structure because it allows the
program to select one path from multiple alternatives.
-Jagannath Kumar Ch
Write an algorithm to find the largest number
between two given numbers
➢ Inputs to the algorithm: ➢ Tracing of Algorithm
Input num1,num2 Given Inputs:
num1 = 50
➢ Expected output:
num2 = 10
The largest number(Maximum Value )
➢ Algorithm ➢ Step-by-Step Execution (Tracing):
Step1: Start 1. Start
Step2: Declare variables num1, num2 2. num1 = 50, num2 = 10
Step3: Read | Input the values for num1, num2
3. Compute IF num1 > num2 THEN
Step4: IF num1 greater than num2 THEN
❖ IF 50 > 10 → True
4.1 Display num1
ELSE ❖ Display num1
4.2 Display num2 ❖ 50
END IF 4. Stop
Step 5: Stop

-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

Step6: Stop 2000 2000 % 400 == 0 Leap Year

2023 2023 % 4 != 0 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)

➢ Inputs to the algorithm:


Input n ( the upper limit, here 100) Tracing :
➢ Expected output: 1. Enter the Values of n =10

Display 1,2,3,……100 2. Assign or initialize i =1


3. Process:
➢ Algorithm
Step 1: Start i i <=10 Test Output
Step 2: Declare variables: n, i 1 1<10 True 1
Step 3: Read the value of n 2 2<10 True 2
Step 4: Initialize variable i - - True -
4.1 Set i←1 10 10<=10 True 10
Step 5: WHILE i Less than or equal to n THEN 11 11<10 False -
5.1: Display i 4. Output:
5.2: Increment i //( set i  i+1)
1,2,3,4,……..10
Step 6: END WHILE
Step7 : Stop
-Jagannath Kumar Ch
For Loop
➢ A FOR loop is used to execute a block of code repeatedly for a specific number of times.
➢ The range of iteration specifies the starting point, ending point, and sometimes the
step or increment of the iteration
➢ A for loop generally contains four parts:
❖ Initialization – setting the starting value of the loop variable.
❖ Condition – the loop runs as long as this condition is true
❖ Body of the loop – the code that needs to be executed repeatedly.
❖ Updation| Increment – updating the loop variable after each iteration
➢ Syntax (general form in most programming languages like C, Java, or Python)

➢ 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 i1
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

❖ Avoids specific language syntax (no semicolons, brackets, etc.)

-Jagannath Kumar Ch
Find the Largest of Two Numbers Calculate the Sum of the First N Numbers
BEGIN BEGIN

INPUT num1, num2 INPUT N

IF num1 > num2 THEN SET sum = 0

PRINT "num1 is larger" FOR i = 1 TO N

ELSE sum = sum + i

PRINT "num2 is larger" ENDFOR

ENDIF PRINT "Sum is", sum

END END

Check if a Number is Even or Odd Print Numbers from 1 to 10 Using a WHILE Loop

BEGIN BEGIN

INPUT number SET counter = 1

IF number MOD 2 = 0 THEN WHILE counter <= 10 DO

PRINT "Number is even" PRINT counter

ELSE counter = counter + 1

PRINT "Number is odd" ENDWHILE

ENDIF END

END
-Jagannath Kumar Ch

You might also like