Chapter 4 – Introduction to Problem Solving
- Write pseudocode that reads two numbers and divide one by another and display the quotient.
START
DECLARE number1, number2 AS FLOAT
DECLARE quotient AS FLOAT
PRINT "Enter first number:"
READ number1
PRINT "Enter second number:"
READ number2
IF number2 == 0 THEN
PRINT "Error: Division by zero."
ELSE
quotient = number1 / number2
PRINT "Quotient:", quotient
END IF
END
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?
START
count1 = 0
count2 = 0
flips = READ input list of 5 integers
FOR flip IN flips DO
IF flip == 1 THEN count1++
ELSE IF flip == 2 THEN count2++
// Check for a winner after each flip
IF count1 == 3 THEN
PRINT "Player 1 wins!"
RETURN
ELSE IF count2 == 3 THEN
PRINT "Player 2 wins!"
RETURN
END FOR
3. Write the pseudocode to print all multiples of 5 between 10 and 25 (including both 10 and 25).
START
FOR number FROM 10 TO 25 DO
IF number MOD 5 == 0 THEN PRINT number
END FOR
END
4. Give an example of a loop that is to be executed a certain number of times.
START
// Define the number of times to execute the loop
NUM_TIMES = 5
// Loop from 1 to NUM_TIMES
FOR i FROM 1 TO NUM_TIMES DO
PRINT i
END FOR
END
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.
START
total_collected = 0
target_amount = 200
WHILE total_collected < target_amount DO
PRINT "Enter the contribution amount:"
READ contribution
total_collected = total_collected + contribution
PRINT "Total collected so far: ", total_collected
END WHILE
PRINT "Congratulations! You have collected ₹200."
END
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
START
// Input: Get price and quantity
PRINT "Enter price:"
READ price
PRINT "Enter quantity:"
READ quantity
// Calculate total bill and GST
total_bill = price * quantity
gst = total_bill * 0.05
total_with_gst = total_bill + gst
// Print results
PRINT "Total Bill: ₹", total_bill
PRINT "GST (5%): ₹", gst
PRINT "Total with GST: ₹", total_with_gst
END
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
START
// Input: Read marks for three subjects
PRINT "Enter marks for Computer Science:"
READ cs_marks
PRINT "Enter marks for Mathematics:"
READ math_marks
PRINT "Enter marks for Physics:"
READ physics_marks
// Calculate total and percentage
total_marks = cs_marks + math_marks + physics_marks
percentage = (total_marks / 300) * 100
// Output results
PRINT "Total Marks:", total_marks
PRINT "Percentage:", percentage, "%"
END
8. Write an algorithm to find the greatest among two different numbers entered by the user.
START
// Step 1: Input
READ num1
READ num2
// Step 2: Comparison
greatest = IF num1 > num2 THEN num1 ELSE num2
// Step 3: Output
PRINT "The greatest number is:", greatest
END
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
START
// Step 1: Input
PRINT "Enter a number:"
READ num
// Step 2: Check Ranges
IF num >= 5 AND num < 15 THEN
PRINT "GREEN"
ELSE IF num >= 15 AND num < 25 THEN
PRINT "BLUE"
ELSE IF num >= 25 AND num < 35 THEN
PRINT "ORANGE"
ELSE
PRINT "ALL COLOURS ARE BEAUTIFUL"
END
10. Write an algorithm that accepts four numbers as input and find the largest and smallest of them.
START
// Step 1: Input
READ num1, num2, num3, num4
// Step 2: Initialize
largest = num1
smallest = num1
// Step 3: Comparison
FOR each num IN (num2, num3, num4) DO
IF num > largest THEN largest = num
IF num < smallest THEN smallest = num
END FOR
// Step 4: Output
PRINT "Largest number is:", largest
PRINT "Smallest number is:", smallest
END
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 .
START
READ units_consumed
meter_charges = 75
IF units_consumed <= 100 THEN
total_bill = units_consumed * 5
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)
total_bill = total_bill + meter_charges
PRINT "Total Water Bill: ₹", total_bill
END
12. What are conditionals? When they are required in a program
What Are Conditionals?
Conditionals are programming constructs that allow a program to execute different code blocks based on whether a specified condition evaluates to true or false. Common types include if, else if, else, and switch statements.
When Are Conditionals Required?
Conditionals are necessary when:
Decision Making: The program needs to make choices based on user input or variable values (e.g., checking eligibility).
Flow Control: They direct the execution flow, enabling the program to respond differently under various circumstances.
Example
if age >= 18:
print("You can vote.")
else:
print("You are too young to vote.")
15. Write a pseudocode to calculate the factorial of a number.(Hint: Factorial of 5, written as 5! = 5 * 4 * 3 * 2 * 1)
START
// Step 1: Input
PRINT "Enter a number:"
READ number
// Step 2: Initialize factorial
factorial = 1
// Step 3: Calculate factorial iteratively
FOR i FROM 1 TO number DO
factorial = factorial * i
END FOR
// Step 4: Output the result
PRINT "Factorial of", number, "is:", factorial
END
16. Following is an algorithm to classify numbers as “Single Digit”, “Double Digit” or “Big”. Classify_Numbers_Algo
INPUT Number
IF Number < 9
“Single Digit”
Else If Number < 99
“Double Digit”
Else “Big”
Verify for (5, 9, 47, 99, 100 200) and correct the algorithm if required
See the correct algorithm below:
START
INPUT Number
IF Number <= 9 THEN
PRINT "Single Digit"
ELSE IF Number <= 99 THEN
PRINT "Double Digit"
ELSE
PRINT "Big"
END
18. For some calculations, we want an algorithm that accepts only positive integers upto 100
Accept_1to100_Algo
INPUT Number IF (0<= Number) AND (Number <= 100)
ACCEPT
Else
REJECT
a) On what values will this algorithm fail?
The algorithm will fail for the following values:
1. 0: The algorithm incorrectly accepts 0 as a valid input, even though it requires only positive integers.
2. Negative Integers: Any negative integer (e.g., -1, -50) will be rejected,
3. Non-integer Values: The algorithm does not handle non-integer inputs
4. Values Greater than 100: Any integer greater than 100 will be rejected
b) Can you improve the algorithm?
START
INPUT Number
// Check if Number is an integer and within the valid range
IF (is_integer(Number)) AND (Number > 0) AND (Number <= 100) THEN
PRINT "ACCEPT"
ELSE
PRINT "REJECT"
END