Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

CHAPTER 4:

INTRODUCTION TO PROBLEM SOLVING

CHAPTER BLUEPRINT PLAN FOR FINAL EXAM

VSA SA LA E TOTAL

1 MARK 2 MARKS 3 MARKS 5 MARKS

PRIMUS PU COLLEGE, BANGALORE


2 MCQ 1 2 1 15 MARKS

MCQ

1. What is the first step in problem-solving as per the textbook?

- A) Testing the solution

- B) Coding the algorithm

- C) Analyzing the problem

- D) Debugging the program

- Answer: C) Analyzing the problem

2. What is an algorithm?

- A) A set of exact steps to solve a problem


- B) A programming language

- C) A type of computer hardware

- D) A method of debugging

- Answer: A) A set of exact steps to solve a problem

3. What does the term "computerisation" refer to?

- A) The use of computers to develop software for automating tasks


PRIMUS PU COLLEGE, BANGALORE

- B) The physical components of a computer system

- C) The process of debugging a program

- D) The network connectivity of computers

- Answer: A) The use of computers to develop software for automating tasks

4. Which of the following is NOT a characteristic of a good algorithm?

- A) Precision

- B) Uniqueness

- C) Infinite steps

- D) Finiteness

- Answer: C) Infinite steps

2
5. What does the process of 'coding' involve?
- A) Writing an algorithm

- B) Testing the program

- C) Converting the algorithm into a format understood by the computer

- D) Debugging the code

- Answer: C) Converting the algorithm into a format understood by the


computer

PRIMUS PU COLLEGE, BANGALORE


6. What shape is used to represent a process in a flowchart?

- A) Parallelogram

- B) Rectangle

- C) Diamond

- D) Circle

- Answer: B) Rectangle

7. In a flowchart, which symbol represents the start or end?

- A) Diamond

- B) Rectangle

- C) Oval

- D) Parallelogram
3
- Answer: C) Oval
8. What is the purpose of developing an algorithm before writing code?

- A) To debug the program

- B) To convert it into machine language

- C) To have a clear solution plan

- D) To test the program

- Answer: C) To have a clear solution plan


PRIMUS PU COLLEGE, BANGALORE

9. Which step comes immediately after developing an algorithm?

- A) Debugging

- B) Testing

- C) Coding

- D) Analyzing the problem

- Answer: C) Coding

10. What does GIGO stand for?

- A) Garbage In, Garbage Out

- B) Good Input, Good Output

- C) Great Input, Great Output


4
- D) Generated Input, Generated Output
- Answer: A) Garbage In, Garbage Out

11. What does 'finiteness' in an algorithm mean?

- A) The algorithm stops after a finite number of steps

- B) The algorithm has infinite steps

- C) The algorithm has unique steps

- D) The algorithm processes data infinitely

PRIMUS PU COLLEGE, BANGALORE


- Answer: A) The algorithm stops after a finite number of steps

12. Which of the following is an essential skill for a computer science student?

- A) Problem-solving

- B) Debugging

- C) Coding

- D) All of the above

- Answer: D) All of the above

13. What is the output of an algorithm?

- A) Input data

- B) A set of instructions
5
- C) The result of the process
- D) A debugging report

- Answer: C) The result of the process

14. What does the parallelogram symbol in a flowchart represent?

- A) Process

- B) Decision

- C) Input/Output
PRIMUS PU COLLEGE, BANGALORE

- D) Start/End

- Answer: C) Input/Output

15. Which of the following steps is NOT part of the problem-solving process
mentioned in the textbook?

- A) Analyzing the problem

- B) Developing an algorithm

- C) Compiling the code

- D) Testing and debugging

- Answer: C) Compiling the code

16. What should be done after finalizing the algorithm?


6
- A) Debugging
- B) Testing

- C) Coding

- D) Analyzing the problem

- Answer: C) Coding

17. Why is it important to textbook the coding procedures?

- A) To make the code run faster

PRIMUS PU COLLEGE, BANGALORE


- B) To understand the logic at a later stage

- C) To avoid writing an algorithm

- D) To reduce the size of the program

- Answer: B) To understand the logic at a later stage

18. What is the role of testing in the problem-solving process?

- A) To analyze the problem

- B) To write the algorithm

- C) To check if the program meets user requirements

- D) To convert the algorithm into code

- Answer: C) To check if the program meets user requirements

7
19. Which of the following is a key component of analyzing a problem?
- A) Writing the program code

- B) Listing the principal components of the problem

- C) Testing the solution

- D) Debugging the program

- Answer: B) Listing the principal components of the problem

20. What is the primary purpose of using an algorithm?


PRIMUS PU COLLEGE, BANGALORE

- A) To debug the program

- B) To test the program

- C) To increase reliability, accuracy, and efficiency

- D) To compile the program

- Answer: C) To increase reliability, accuracy, and efficiency

21. What does the diamond shape in a flowchart represent?

- A) Process

- B) Decision

- C) Input/Output

- D) Start/End

- Answer: B) Decision
8
22. Which programming language can be used to write a program after
developing an algorithm?

- A) Only Python

- B) Any high-level language

- C) Only Java

- D) Only C++

- Answer: B) Any high-level language

PRIMUS PU COLLEGE, BANGALORE


23. What must be done if the program generates incorrect output?

- A) Analyze the problem

- B) Develop a new algorithm

- C) Check for logical errors

- D) Ignore the output

- Answer: C) Check for logical errors

24. Why is a roadmap important for a programmer?

- A) To debug the program

- B) To clearly visualize the instructions to be written

- C) To reduce the size of the program


9
- D) To increase the speed of the program
- Answer: B) To clearly visualize the instructions to be written

25. What is the function of a 'terminator' symbol in a flowchart?

- A) To represent a decision point

- B) To indicate the start or end of the process

- C) To show data input/output

- D) To represent a process or action


PRIMUS PU COLLEGE, BANGALORE

- Answer: B) To indicate the start or end of the process

26. What should be done after coding the algorithm?

- A) Analyzing the problem

- B) Testing and debugging

- C) Developing the algorithm

- D) Writing the problem statement

- Answer: B) Testing and debugging

27. Why is precision important in an algorithm?

- A) To reduce the number of steps

- B) To ensure the steps are clearly stated and defined


10
- C) To make the algorithm run faster
- D) To avoid using high-level language

- Answer: B) To ensure the steps are clearly stated and defined

28. Which step in problem-solving involves identifying the logical steps to reach
a solution?

- A) Analyzing the problem

- B) Developing the algorithm

PRIMUS PU COLLEGE, BANGALORE


- C) Coding

- D) Testing and debugging

- Answer: B) Developing the algorithm

29. Which shape in a flowchart represents input or output data?

- A) Oval

- B) Rectangle

- C) Parallelogram

- D) Diamond

- Answer: C) Parallelogram

30. Why is uniqueness important in an algorithm?


11
- A) To ensure each step is uniquely defined
- B) To make the algorithm run faster

- C) To reduce the number of steps

- D) To avoid using high-level language

- Answer: A) To ensure each step is uniquely defined

31. What is the primary objective of problem-solving in computer science?

- A) To debug the program


PRIMUS PU COLLEGE, BANGALORE

- B) To automate tasks efficiently

- C) To reduce the size of the program

D) To increase the speed of the program

- Answer: B) To automate tasks efficiently

32. In a flowchart, what is used to represent a decision?

- A) Oval

- B) Rectangle

- C) Diamond

- D) Parallelogram
12
- Answer: C) Diamond
33. What should be the next step if an algorithm is not producing the correct
output?

- A) Ignoring the error

- B) Rewriting the problem statement

- C) Revising the algorithm to check for logical errors

- D) Converting it into a flowchart

PRIMUS PU COLLEGE, BANGALORE


- Answer: C) Revising the algorithm to check for logical errors

34. Why is it necessary to document the coding process?

- A) To write more complex code

- B) To ensure the code is understandable and maintainable

- C) To reduce the size of the code

- D) To avoid using high-level language

- Answer: B) To ensure the code is understandable and maintainable

35. What role does testing play in the problem-solving process?

- A) To identify any logical errors in the algorithm

- B) To write the final algorithm


13
- C) To analyze the problem statement
- D) To convert the algorithm into code

- Answer: A) To identify any logical errors in the algorithm

36. What is the purpose of a flowchart?

- A) To represent an algorithm visually

- B) To write the code

- C) To debug the program


PRIMUS PU COLLEGE, BANGALORE

- D) To test the solution

- Answer: A) To represent an algorithm visually

37. Which of the following is NOT a step in the problem-solving process?

- A) Analyzing the problem

- B) Writing the program

- C) Testing and debugging

- D) Developing the algorithm

- Answer: B) Writing the program

38. What is 'finiteness' in the context of an algorithm?

- A) The algorithm must have a finite number of steps


14
- B) The algorithm must run indefinitely
- C) The algorithm should have unique steps

- D) The algorithm must have infinite steps

- Answer: A) The algorithm must have a finite number of steps

39. Why is it necessary to debug a program?

- A) To increase the size of the code

- B) To ensure the program runs without errors

PRIMUS PU COLLEGE, BANGALORE


- C) To write the algorithm

- D) To develop the problem statement

- Answer: B) To ensure the program runs without errors

40. What does a rectangle represent in a flowchart?

- A) Decision

- B) Input/Output

- C) Start/End

- D) Process

- Answer: D) Process

41. Which step involves checking if a program meets user requirements?


15
- A) Analyzing the problem
- B) Developing the algorithm

- C) Coding

- D) Testing

- Answer: D) Testing

42. What is the outcome of an algorithm?

- A) Analyzing the problem


PRIMUS PU COLLEGE, BANGALORE

- B) Writing the program

- C) Producing a result

- D) Debugging the program

- Answer: C) Producing a result

43. What is the significance of 'precision' in an algorithm?

- A) To reduce the number of steps

- B) To ensure each step is clearly defined

- C) To make the algorithm faster

- D) To avoid using high-level language

- Answer: B) To ensure each step is clearly defined

16
44. Why is it important to develop an algorithm before coding?
- A) To reduce the size of the program

- B) To have a clear solution plan

- C) To debug the code

- D) To increase the speed of the program

- Answer: B) To have a clear solution plan

45. What is the role of testing in programming?

PRIMUS PU COLLEGE, BANGALORE


- A) To analyze the problem

- B) To write the code

- C) To identify and fix errors

- D) To develop the algorithm

- Answer: C) To identify and fix errors

46. What shape represents the start or end in a flowchart?

- A) Rectangle

- B) Parallelogram

- C) Oval

- D) Diamond

- Answer: C) Oval
17
47. Which of the following best describes 'uniqueness' in an algorithm?

- A) Each step is distinct and clearly defined

- B) The algorithm has infinite steps

- C) The algorithm runs indefinitely

- D) The algorithm is written in a high-level language

- Answer: A) Each step is distinct and clearly defined


PRIMUS PU COLLEGE, BANGALORE

48. What should be done if a program produces unexpected results?

- A) Ignore the results

- B) Re-analyze the problem

- C) Debug and test the program

- D) Write a new algorithm

- Answer: C) Debug and test the program

49. What does the term 'coding' refer to in problem-solving?

- A) Writing the problem statement

- B) Converting the algorithm into a programming language

- C) Testing the program

- D) Analyzing the problem


18
- Answer: B) Converting the algorithm into a programming language
50. Why is 'debugging' an essential part of problem-solving in programming?

- A) To write the algorithm

- B) To fix errors in the code

- C) To reduce the size of the program

- D) To develop the problem statement

- Answer: B) To fix errors in the code

PRIMUS PU COLLEGE, BANGALORE


2 Marks Questions

1. Define an algorithm. An algorithm is a finite set of instructions or a step-by-


step procedure for solving a problem or performing a task.
2. What is pseudocode? Pseudocode is a non-formal language that helps
programmers to write algorithms. It is a detailed description of instructions that
a computer must follow in a particular order and is intended for human reading.
3. Explain the term "flowchart." A flowchart is a graphical representation of
the steps in an algorithm or a process, using various symbols to denote different
types of actions or steps.
4. What are conditionals in programming? Conditionals are statements that
allow a program to take different actions based on whether a certain condition
is true or false. They are used to check possibilities and perform operations
19
depending on the outcome of the condition.
5. What is the purpose of a flowchart? The purpose of a flowchart is to visually
represent the sequence of steps in a process or algorithm, making it easier to
understand and analyze the flow of control and the logic of the process.
6. Describe the term "sequence" in the context of algorithms. In the context of
algorithms, "sequence" refers to the execution of steps one after the other in a
specific order, without any branching or repetition.
7. What does iterative mean in algorithm design? In algorithm design,
"iterative" refers to the process of repeating a set of instructions a certain
number of times or until a specific condition is met. This repetition is known as
PRIMUS PU COLLEGE, BANGALORE

a loop.
8. Why is it important to verify an algorithm? It is important to verify an
algorithm to ensure that it correctly and efficiently solves the problem it is
intended to address and to confirm that it produces the correct output for all
possible inputs.
9. What is a loop in programming? A loop in programming is a control structure
that allows the repeated execution of a block of code as long as a specified
condition is met.
10. What is the significance of decision making in algorithms? Decision making
in algorithms is significant because it allows the algorithm to choose different
paths of execution based on certain conditions, enabling it to handle a variety
of situations and inputs.
11. Write pseudocode to add two numbers. Pseudocode for adding two numbers:

INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
20
12. Define "space complexity" in algorithm analysis. Space complexity in
algorithm analysis refers to the amount of memory space an algorithm needs to
run to completion, taking into account both the fixed and variable space
requirements.
13. Define "time complexity" in algorithm analysis. Time complexity in
algorithm analysis refers to the amount of time an algorithm takes to run as a
function of the length of the input. It helps to estimate the efficiency of the
algorithm.
14. What are the components of a flowchart? The components of a flowchart

PRIMUS PU COLLEGE, BANGALORE


include various symbols such as:

• Ovals for start and end points


• Rectangles for processing steps
• Diamonds for decision points
• Arrows for flow of control

15. Explain the difference between pseudocode and a flowchart. Pseudocode is


a textual representation of an algorithm written in a human-readable format,
while a flowchart is a graphical representation of the same algorithm, using
symbols to depict different types of actions and the flow of control.
16. Describe the term "branching" in algorithms. Branching in algorithms refers
to the process of making decisions within the algorithm, where the flow of
control can take different paths based on certain conditions.
17. What is the role of an algorithm in problem-solving? The role of an
algorithm in problem-solving is to provide a clear, step-by-step procedure to
solve a specific problem, ensuring that the solution is correct, efficient, and can
be executed by a computer program. 21
3 Marks Questions

1. Write an algorithm to find the greatest of two numbers.


o Algorithm:
1. Start
2. Input number1
3. Input number2
4. If number1 > number2, then
▪ Print "number1 is greater"
PRIMUS PU COLLEGE, BANGALORE

5. Else
▪ Print "number2 is greater"
6. End
2. Explain the importance of pseudocode before actual coding.
o Importance of Pseudocode:
▪ Helps in planning and organizing thoughts.
▪ Ensures all steps of the algorithm are considered.
▪ Easy to read and understand.
▪ Bridges the gap between human logic and programming
language syntax.
▪ Facilitates debugging by identifying logical errors before
coding.
3. Describe the process of writing a flowchart for a given algorithm.
o Process of Writing a Flowchart:
▪ Identify the major steps in the algorithm.
▪ Use standard flowchart symbols (ovals for start/end, rectangles
for processes, diamonds for decisions).
22
▪ Connect the symbols with arrows to show the flow of control.
▪ Ensure the flowchart is logically correct and covers all scenarios.
▪ Review and refine the flowchart for accuracy and clarity.
4. How can you improve an algorithm that fails for certain inputs?
o Improving an Algorithm:
▪ Identify the inputs that cause the algorithm to fail.
▪ Analyze the algorithm to understand why it fails for those inputs.
▪ Modify the algorithm to handle those specific cases.
▪ Test the algorithm with a variety of inputs to ensure robustness.
▪ Optimize the algorithm to improve efficiency and performance.
5. Write pseudocode for calculating the area and perimeter of a rectangle.

PRIMUS PU COLLEGE, BANGALORE


o Pseudocode:

INPUT length
INPUT width
COMPUTE area = length * width
COMPUTE perimeter = 2 * (length + width)
PRINT area
PRINT perimeter

6. Explain the importance of conditionals with an example.


o Importance of Conditionals:
▪ Allow the program to make decisions based on certain
conditions.
▪ Enable the program to execute different actions based on
different inputs.
▪ Example: In a temperature control system, if the temperature is
below 20°C, the heater turns on; if it is above 25°C, the air
conditioner turns on.
7. Describe the algorithm for checking whether a number is within a specific 23
range.
o Algorithm:
1. Start
2. Input number
3. Input lower_bound
4. Input upper_bound
5. If number >= lower_bound and number <= upper_bound, then
▪ Print "number is within the range"
6. Else
▪ Print "number is out of range"
PRIMUS PU COLLEGE, BANGALORE

7. End
8. Explain how to handle time addition in pseudocode when minutes exceed
60.
o Pseudocode:

INPUT hours1, minutes1


INPUT hours2, minutes2
total_minutes = minutes1 + minutes2
extra_hours = total_minutes / 60
remaining_minutes = total_minutes % 60
total_hours = hours1 + hours2 + extra_hours
PRINT total_hours, remaining_minutes

9. Discuss the significance of decision-making algorithms with a real-life


example.
o Significance:
▪ Decision-making algorithms allow systems to choose between
different actions based on conditions.

24 ▪ Example: In an e-commerce application, a recommendation


system suggests products based on user preferences and past
purchases, improving user experience and sales.
10. What steps should be taken if an algorithm has multiple approaches?
o Steps:
▪ Analyze each approach for correctness and efficiency.
▪ Compare the approaches based on their time and space
complexity.
▪ Test each approach with various inputs to identify the best one.
▪ Choose the most efficient and robust approach.
▪ Document the reasons for choosing that particular approach.
11. Explain the importance of verifying an algorithm with an example.

PRIMUS PU COLLEGE, BANGALORE


o Importance:
▪ Verifying ensures the algorithm produces correct results for all
inputs.
▪ Example: In a sorting algorithm, verification involves checking
that the output list is sorted for different input lists, including
edge cases like empty lists or lists with repeated elements.
12. Describe the pseudocode to classify numbers as "Single Digit," "Double
Digit," or "Big."
o Pseudocode:

typescript
Copy code
INPUT number
IF number >= 0 AND number <= 9 THEN
PRINT "Single Digit"
ELSE IF number >= 10 AND number <= 99 THEN
PRINT "Double Digit"
ELSE
PRINT "Big" 25

13. Write an algorithm to calculate the water bill based on consumption.


o Algorithm:
1. Start
2. Input consumption
3. If consumption <= 30, then
▪ bill = consumption * 2
4. Else if consumption <= 50, then
▪ bill = 30 * 2 + (consumption - 30) * 3
5. Else
▪ bill = 30 * 2 + 20 * 3 + (consumption - 50) * 5
PRIMUS PU COLLEGE, BANGALORE

6. Print bill
7. End
14. What improvements can be made to an algorithm that only accepts positive
integers up to 100?
o Improvements:
▪ Add input validation to check if the input is within the range.
▪ Provide feedback to the user if the input is out of range.
▪ Optimize the algorithm to handle edge cases.
▪ Ensure the algorithm handles different data types correctly.
▪ Include error handling to manage invalid inputs gracefully.
15. Explain the algorithm for determining the winner in a coin-flipping game.
o Algorithm:
1. Start
2. Input number_of_flips
3. Initialize heads_count and tails_count to 0
4. For each flip, do
▪ If the result is heads, increment heads_count
26
▪ Else, increment tails_count
5. If heads_count > tails_count, then
▪ Print "Heads win"
6. Else if tails_count > heads_count, then
▪ Print "Tails win"
7. Else
▪ Print "It's a tie"
8. End
16. Write the pseudocode to print all multiples of 5 between 10 and 25.
o Pseudocode:

PRIMUS PU COLLEGE, BANGALORE


FOR number FROM 10 TO 25
IF number % 5 == 0 THEN
PRINT number

17. How do you write an algorithm to read the marks of three subjects and
calculate the aggregate?
o Algorithm:
1. Start
2. Input marks1, marks2, marks3
3. aggregate = marks1 + marks2 + marks3
4. Print aggregate
5. End
18. What is the significance of iterative steps in an algorithm?
o Significance:
▪ Iterative steps allow the repetition of certain actions until a
condition is met.
▪ They help in simplifying complex problems by breaking them
down into repetitive tasks.
27
▪ Improve the efficiency of algorithms by reducing redundancy.
▪ Essential for tasks like searching, sorting, and processing data in
loops.
▪ Enable handling of large datasets and continuous processes.
19. Describe the process of designing an algorithm to add hours and minutes.
o Process:
1. Start
2. Input hours1, minutes1
3. Input hours2, minutes2
4. total_minutes = minutes1 + minutes2
PRIMUS PU COLLEGE, BANGALORE

5. extra_hours = total_minutes / 60
6. remaining_minutes = total_minutes % 60
7. total_hours = hours1 + hours2 + extra_hours
8. Print total_hours, remaining_minutes
9. End

5 Marks Questions (15 questions)

1. Write a detailed algorithm to determine the winner in a coin-flipping game.


o Algorithm:
1. Start
2. Input the number of coin flips (n)
3. Initialize counters: player1_wins = 0, player2_wins = 0
4. For each flip from 1 to n:
▪ Flip the coin and get the result (heads or tails)
28 ▪ If the result is heads, increment player1_wins
▪ If the result is tails, increment player2_wins
5. If player1_wins > player2_wins, then:
▪ Print "Player 1 wins"
6. Else if player2_wins > player1_wins, then:
▪ Print "Player 2 wins"
7. Else:
▪ Print "It's a tie"
8. End
2. Explain the importance of flowcharts in problem-solving.
o Importance of Flowcharts:
▪ Visual Representation: Provides a clear and visual way to

PRIMUS PU COLLEGE, BANGALORE


understand the flow of a process.
▪ Simplifies Complex Processes: Breaks down complex processes
into understandable parts.
▪ Identifies Bottlenecks: Helps in pinpointing inefficiencies or
bottlenecks in a process.
▪ Facilitates Communication: Acts as a common language for
team members from different backgrounds.
▪ Example: A flowchart for a customer service process can help
identify steps where delays occur, improving efficiency.
3. Describe the algorithm for adding hours and minutes, including the
necessary conditionals.
o Algorithm:
1. Start
2. Input hours1, minutes1
3. Input hours2, minutes2
4. total_minutes = minutes1 + minutes2
5. extra_hours = total_minutes / 60 29
6. remaining_minutes = total_minutes % 60
7. total_hours = hours1 + hours2 + extra_hours
8. If total_hours >= 24, then:
▪ total_hours = total_hours % 24
9. Print total_hours, remaining_minutes
10. End
4. Write the pseudocode and draw the flowchart for calculating the area and
perimeter of a rectangle.
o Pseudocode:

INPUT length
INPUT width
PRIMUS PU COLLEGE, BANGALORE

COMPUTE area = length * width


COMPUTE perimeter = 2 * (length + width)
PRINT area
PRINT perimeter

o Flowchart:

30
PRIMUS PU COLLEGE, BANGALORE
5. Explain the importance of verifying algorithms with multiple examples.
o Importance:
▪ Ensures Correctness: Verification confirms that the algorithm
produces the correct output for various inputs.
▪ Identifies Edge Cases: Helps in discovering and handling edge
cases that might not be apparent initially.
▪ Improves Reliability: Builds confidence in the algorithm's
reliability and robustness.
▪ Enhances Understanding: Multiple examples provide a deeper 31
understanding of how the algorithm works.
▪ Reduces Errors: Early detection of errors prevents issues later in
the development process.
6. Write a detailed algorithm to classify numbers as "Single Digit," "Double
Digit," or "Big."
o Algorithm:
1. Start
2. Input number
3. If number >= 0 and number <= 9, then:
▪ Print "Single Digit"
PRIMUS PU COLLEGE, BANGALORE

4. Else if number >= 10 and number <= 99, then:


▪ Print "Double Digit"
5. Else:
▪ Print "Big"
6. End
7. Describe the steps involved in writing an algorithm for the total water bill
calculation.
o Steps:
1. Start
2. Input water consumption
3. If consumption <= 30, then:
▪ bill = consumption * 2
4. Else if consumption > 30 and <= 50, then:
▪ bill = (30 * 2) + ((consumption - 30) * 3)
5. Else:
▪ bill = (30 * 2) + (20 * 3) + ((consumption - 50) * 5)

32 6. Print bill
7. End
8. Explain the process of improving an algorithm that fails for certain inputs.
o Process:
1. Identify the inputs causing the failure.
2. Analyze the algorithm to understand why it fails for these inputs.
3. Modify the algorithm to handle these specific cases.
4. Test the modified algorithm with a variety of inputs.
5. Example: If a sorting algorithm fails for an array containing
negative numbers, modify the algorithm to include handling of
negative values, then test it with arrays containing negative
numbers.

PRIMUS PU COLLEGE, BANGALORE


9. Write a detailed pseudocode to calculate the factorial of a number.
o Pseudocode:

INPUT number
IF number == 0 THEN
factorial = 1
ELSE
factorial = 1
FOR i FROM 1 TO number
factorial = factorial * i
PRINT factorial

10. Describe the process of writing an algorithm that accepts only positive
integers up to 100.
o Process:
1. Start
2. Input number
3. If number < 1 or number > 100, then:
▪ Print "Invalid input"
33
4. Else:
▪ Print "Valid input"
5. End
11. Explain the steps involved in designing an algorithm for a coin-flipping
game with detailed conditionals.
o Steps:
1. Start
2. Input the number of flips
3. Initialize counters for heads and tails
4. For each flip:
▪ If heads, increment heads counter
PRIMUS PU COLLEGE, BANGALORE

▪ If tails, increment tails counter


5. If heads counter > tails counter, then:
▪ Print "Heads win"
6. Else if tails counter > heads counter, then:
▪ Print "Tails win"
7. Else:
▪ Print "It's a tie"
8. End
12. Write a detailed algorithm to print all multiples of 5 between 10 and 25.
o Algorithm:
1. Start
2. For number from 10 to 25:
▪ If number % 5 == 0, then:
▪ Print number
3. End
13. Describe the process of writing an algorithm to read the marks of three

34 subjects and calculate the percentage.


o Process:
1. Start
2. Input marks for subject1, subject2, and subject3
3. total_marks = subject1 + subject2 + subject3
4. percentage = (total_marks / 300) * 100
5. Print percentage
6. End
14. Explain the significance of iterative and branching steps in algorithm
design with examples.
o Significance:
▪ Iterative Steps: Allow repetitive actions, simplifying the process

PRIMUS PU COLLEGE, BANGALORE


of handling multiple data elements. Example: Looping through
an array to calculate the sum of its elements.
▪ Branching Steps: Enable decision-making by allowing different
actions based on conditions. Example: Using if-else statements
to handle different cases in a grading system.
▪ Enhance Efficiency: Iterative steps reduce redundancy, while
branching steps handle diverse scenarios, making the algorithm
robust and efficient.
▪ Improve Clarity: Clearly defined iterative and branching steps
make the algorithm easier to understand and maintain.
▪ Adapt to Complex Problems: Combine iterative and branching
steps to solve complex problems by breaking them into
manageable parts.
15. Explain the steps involved in Problem Solving.

35
PRIMUS PU COLLEGE, BANGALORE

1. Analysing the Problem:


o Understand the problem thoroughly before seeking a solution.
o Carefully read and analyze the problem statement.
o List the principal components and decide the core functionalities of the
solution.
o Identify the inputs and expected outputs.
2. Developing an Algorithm:
o Devise a solution plan represented in natural language, called an
algorithm.
o Refine the algorithm until it captures all aspects of the desired solution.
o Select the most suitable algorithm if multiple solutions are possible.
3. Coding:
o Convert the finalized algorithm into a format understood by the
36
computer using a high-level programming language.
o Record coding procedures and document the solution for future
reference and maintenance.
4. Testing and Debugging:
o Test the program to ensure it meets user requirements and responds
within the expected time.
o Verify that the program generates correct output for all possible inputs.
o Fix any syntactical or logical errors encountered during testing.
o Follow standard testing methods like unit, integration, system, and
acceptance testing to ensure the software meets all business and

PRIMUS PU COLLEGE, BANGALORE


technical requirements.
o Debug the program to rectify any defects found during testing.

16. Explain the characteristics of a good algorithm.


Characteristics of a good algorithm
• Precision — the steps are precisely stated or defined.
• Uniqueness — results of each step are uniquely defined and only depend on
the input and the result
of the preceding steps.
• Finiteness — the algorithm always stops after a finite number of steps.
• Input — the algorithm receives some input.
• Output — the algorithm produces some output.

37
EXERCISES (CHAPTER END QUESTIONS)

Question 1: Write pseudocode that reads two numbers and divides one by another and
displays the quotient.

Answer 1:

Start
Input number1
Input number2
If number2 is not 0, then
PRIMUS PU COLLEGE, BANGALORE

Set quotient = number1 / number2


Print quotient
Else
Print "Division by zero is not allowed."
End

Question 2: Two friends decide who gets the last slice of a cake by flipping a coin five
times. The first person to win three flips wins the cake. An input of 1 means player 1
wins a flip, and a 2 means player 2 wins a flip. Design an algorithm to determine who
takes the cake?

Answer 2:

Start
Initialize player1_wins to 0
Initialize player2_wins to 0
For i from 1 to 5 do
Input flip_result
If flip_result is 1 then
38 Increment player1_wins
Else If flip_result is 2 then
Increment player2_wins
End If
If player1_wins is 3 then
Print "Player 1 wins the cake"
Exit
Else If player2_wins is 3 then
Print "Player 2 wins the cake"
Exit
End If
End For
End

PRIMUS PU COLLEGE, BANGALORE


Question 3: Write the pseudocode to print all multiples of 5 between 10 and 25
(including both 10 and 25).

Answer 3:

Start
For number from 10 to 25 do
If number mod 5 is 0 then
Print number
End If
End For
End

Question 4: Give an example of a loop that is to be executed a certain number of times.

Answer 4:

Start
For i from 1 to 10 do
Print "This is iteration number " + i
End For 39
End
Question 5: Suppose you are collecting money for something. You need ₹200 in all.
You ask your parents, uncles, and aunts as well as grandparents. Different people may
give either ₹10, ₹20, or even ₹50. You will collect till the total becomes 200. Write the
algorithm.

Answer 5:

Start
Initialize total_collected to 0
While total_collected < 200 do
PRIMUS PU COLLEGE, BANGALORE

Input amount_given
If amount_given is 10 or 20 or 50 then
total_collected = total_collected + amount_given
End If
End While
Print "Total collected: " + total_collected
End

Question 6: Write the pseudocode to print the bill depending upon the price and
quantity of an item. Also print Bill GST, which is the bill after adding 5% of tax in the
total bill.

Answer 6:

Start
Input price
Input quantity
Set total_bill = price * quantity
Set gst = total_bill * 0.05
Set bill_gst = total_bill + gst
40 Print "Total Bill: " + total_bill
Print "Bill after GST: " + bill_gst
End
Question 7: Write pseudocode that will perform the following: a) Read the marks of
three subjects: Computer Science, Mathematics, and Physics, out of 100. b) Calculate
the aggregate marks. c) Calculate the percentage of marks.

Answer 7:

Start
Input computer_science_marks
Input mathematics_marks
Input physics_marks

PRIMUS PU COLLEGE, BANGALORE


Set aggregate_marks = computer_science_marks + mathematics_marks +
physics_marks
Set percentage_marks = (aggregate_marks / 300) * 100
Print "Aggregate Marks: " + aggregate_marks
Print "Percentage Marks: " + percentage_marks
End

Question 8: Write an algorithm to find the greatest among two different numbers
entered by the user.

Answer 8:

Start
Input number1
Input number2
If number1 > number2 then
Print "The greatest number is " + number1
Else
Print "The greatest number is " + number2
End

41
Question 9: Write an algorithm that performs the following: Ask a user to enter a
number. If the number is between 5 and 15, write the word GREEN. If the number is
between 15 and 25, write the word BLUE. If the number is between 25 and 35, write
the word ORANGE. If it is any other number, write that ALL COLOURS ARE
BEAUTIFUL.

Answer 9:

Start
Input number
If number >= 5 and number <= 15 then
Print "GREEN"
PRIMUS PU COLLEGE, BANGALORE

Else If number > 15 and number <= 25 then


Print "BLUE"
Else If number > 25 and number <= 35 then
Print "ORANGE"
Else
Print "ALL COLOURS ARE BEAUTIFUL"
End

Question 10: Write an algorithm that accepts four numbers as input and find the largest
and smallest of them.

Answer 10:

pseudocode
Copy code
Start
Input number1
Input number2
Input number3
Input number4
Set largest = number1
42
Set smallest = number1
If number2 > largest then
Set largest = number2
If number3 > largest then
Set largest = number3
If number4 > largest then
Set largest = number4
If number2 < smallest then
Set smallest = number2
If number3 < smallest then
Set smallest = number3
If number4 < smallest then
Set smallest = number4

PRIMUS PU COLLEGE, BANGALORE


Print "Largest number is " + largest
Print "Smallest number is " + smallest
End

Question 11: Write an algorithm to display the total water bill charges of the month
depending upon the number of units consumed by the customer as per the following
criteria:

• for the first 100 units @ ₹5 per unit


• for next 150 units @ ₹10 per unit
• more than 250 units @ ₹20 per unit Also add meter charges of ₹75 per month
to calculate the total water bill.

Answer 11:

Start
Input units_consumed
Set meter_charges = 75
If units_consumed <= 100 then
total_bill = units_consumed * 5 43
Else If units_consumed <= 250 then
total_bill = (100 * 5) + ((units_consumed - 100) * 10)
Else
total_bill = (100 * 5) + (150 * 10) + ((units_consumed - 250) *
20)
End If
total_bill = total_bill + meter_charges
Print "Total Water Bill: " + total_bill
End

Question 12: What are conditionals? When are they required in a program?

Answer 12: Conditionals are statements that perform different actions based on
PRIMUS PU COLLEGE, BANGALORE

whether a given condition is true or false. They are required in a program to make
decisions and execute certain sections of code depending on specific conditions.

Question 13:

Answer 13:

44
→ Flow of control
Question 14: Following is an algorithm for going to school or college. Can you suggest
improvements in this to include other options? Reach_School_Algorithm a) Wake up
b) Get ready c) Take lunch box d) Take bus e) Get off the bus f) Reach school or college

Answer 14:

Reach_School_Algorithm
a) Wake up
b) Get ready
c) Take lunch box

PRIMUS PU COLLEGE, BANGALORE


d) If going by bus then
Take bus
Get off the bus
Else If going by car then
Get in the car
Get out of the car
Else If walking then
Walk to school or college
End If
f) Reach school or college

Question 15: Write a pseudocode to calculate the factorial of a number. (Hint: Factorial
of 5, written as 5! = 5 × 4 × 3 × 2 × 1).

Answer 15:

Start
Input number
Set factorial = 1
For i from 1 to number do
factorial = factorial * i
45
End For
Print "Factorial of " + number + " is " + factorial
End

Question 16: Draw a flowchart to check whether a given number is an Armstrong


number. An Armstrong number of three digits is an integer such that the sum of the
cubes of its digits is equal to the number itself.

Answer 16:
PRIMUS PU COLLEGE, BANGALORE

46
PRIMUS PU COLLEGE, BANGALORE

47
Question 17: Following is an algorithm to classify numbers as “Single Digit”, “Double
Digit” or “Big”. Verify for (5, 9, 47, 99, 100, 200) and correct the algorithm if required.

Answer 17:

Classify_Numbers_Algo
INPUT Number
IF Number < 10 then
Print "Single Digit"
Else If Number < 100 then
Print "Double Digit"
PRIMUS PU COLLEGE, BANGALORE

Else
Print "Big"
End If

Verification:
For 5, 9, 47, 99, 100, 200:
- 5 and 9 should be classified as "Single Digit"
- 47 and 99 should be classified as "Double Digit"
- 100 and 200 should be classified as "Big"

Question 18: For some calculations, we want an algorithm that accepts only positive
integers up to 100.

Accept_1to100_Algo
INPUT Number
IF (0 < Number) AND (Number <= 100) then
ACCEPT
Else
REJECT
End If
48

a) On what values will this algorithm fail? b) Can you improve the algorithm?
Answer 18: a) This algorithm will fail for the value 0 because 0 is not considered a
positive integer. b) Improved algorithm:

Accept_1to100_Algo
INPUT Number
IF (Number > 0) AND (Number <= 100) then
ACCEPT
Else
REJECT
End If

PRIMUS PU COLLEGE, BANGALORE


s********

49

You might also like