Problem Solving with Python Lab
Week 2
2.1 Fibonacci sequence Generation
Write an algorithm, flowchart and pseudocode that generates the first ‘n’ terms of the
Fibonacci sequence.
Example: Generate the first 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
Algorithm:
Step 1: Start
Step 2: Declare variable n1, n2, sum, n, i
Step 3: Initialize variables: n1 = 0, n2 = 1, i = 2
Step 4: Read n
Step 5: Repeat this step until i < n:
sum = n1 + n2
print sum
n1 = n2
n2 = sum
i=i+1
Step 6: Stop
Flowchart:
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Pseudocode:
BEGIN
Declare n1, n2, sum, n, i
Initialize n1 = 0
Initialize n2 to 1
Initialize sum to 0
Input n
Initialize i to 2
Print n1, n2
While i < n do
calculate sum as n1 + n2
print sum
set n1 to n2
set n2 to sum
increment i by 1
END WHILE
END
Program:
n1 = 0
n2 = 1
sum = 0
n = int(input("Enter a number: "))
i=2
print(n1, n2, end=' ')
while i < n:
sum = n1 + n2
print(sum, end=' ')
n1 = n2
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
n2 = sum
i=i+1
Output:
Enter a number: 10
0 1 1 2 3 5 8 13 21 34
2.2 Exponentiation of a Number
Develop an algorithm, flowchart and pseudocode to compute the result of a number raised
to the power of another integer.
Example: Calculate 4^3 and (1.5) ^3.
Algorithm:
Step 1: Start
Step 2: Declare base, exp
Step 3: Read base and exp
Step 4: Initialize res = 1 and count = 0
Step 5: While count < exp:
result *= base
count += 1
Step 6: Repeat Step 5 until the condition fails
Step 7: Print the value of result
Step 8: Stop
Flowchart:
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Pseudocode:
BEGIN
DECLARE base, exp
READ base, expo
res = 1
count = 0
WHILE count < expo DO
res = res * base
count = count + 1
END WHILE
PRINT res
END
Program:
base = float(input("Enter the base: "))
exp = int(input("Enter the exponent: "))
res = 1
count = 0
while count < exp:
res *= base
count += 1
print(f"{base}^{exp} =", res)
Output:
Enter the base: 4 Enter the base: 1.5
Enter the exponent: 3 Enter the exponent: 3
4.0^3 = 64.0 1.5^3 = 3.375
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
2.3 Reversing the Elements of an Array
Design an algorithm, flowchart and pseudocode to reverse the elements in a given array.
Example: Reverse the array [1, 2, 3, 4, 5] to [5, 4, 3, 2, 1].
Algorithm:
Step 1: Start
Step 2: Initialize the array arr with values [1, 2, 3, 4, 5]
Step 3: Print "Original array:"
Step 4: Initialize i = 0
Step 5: While i is less than the length of arr:
Print arr[i]
Increment i by 1
Step 6: Print "Array in reverse order:"
Step 7: Initialize i to the length of arr - 1
Step 8: While i is greater than or equal to 0:
Print arr[i]
Decrement i by 1
Step 9: Stop
Flowchart:
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Pseudocode:
BEGIN
DECLARE arr, i
arr = [1, 2, 3, 4, 5]
PRINT "Original array:"
i=0
WHILE i < length(arr) DO
PRINT arr[i]
i=i+1
END WHILE
PRINT "Array in reverse order:"
i = length(arr) - 1
WHILE i >= 0 DO
PRINT arr[i]
i=i-1
END WHILE
END
Program:
arr = [1, 2, 3, 4, 5]
print("Original array:")
i=0
while i < len(arr):
print(arr[i], end=' ')
i += 1
print("\nArray in reverse order:")
i = len(arr) - 1
while i >= 0:
print(arr[i], end=' ')
i -= 1
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Output:
Original array:
12345
Array in reverse order:
54321
2.4 Finding the Largest Number in an Array
Write an algorithm, flowchart and pseudocode to find the largest number in a given array.
Example: Find the largest number in the array [150, 160, 170, 180].
Algorithm:
Step 1: Start
Step 2: Declare array a[n] and variable i, large
Step 3: Read n from User and read all the elements of a[n]
Step 4: Initialize Variable i=1 and large=a[0]
Step 5: Repeat Until i<=n-1
if(a[i]>large), set large=a[i]
increment i=i+1
Step 6: Print "The largest element is": large
Step 7: Stop
Pseudocode:
BEGIN
DECLARE a, large, i, n
READ a
n = length of a
large = a[0]
i=1
WHILE i < n DO
IF a[i] > large THEN
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
large = a[i]
END IF
i=i+1
END WHILE
PRINT large
END
Flowchart:
Program:
a = [150, 160, 170, 180]
n = len(a)
large = a[0]
i=1
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
while i < n:
if a[i] > large:
large = a[i]
i += 1
print("The largest element in the array is: ", large)
Output:
The largest element in the array is: 180
2.5 Multiplication of Two Matrices
Create an algorithm, flowchart and pseudocode to multiply two matrices. Solve the
following examples
𝟏 𝟐 𝟓 𝟔
a. [ ]*[ ]
𝟑 𝟒 𝟕 𝟖
𝟓 𝟔 𝟕 𝟏 𝟐
b. [ ]*[𝟏𝟎 𝟏𝟏]
𝟖 𝟗 𝟏𝟎
𝟏𝟑 𝟏𝟒
Algorithm:
Step 1: Start
Step 2: Declare matrix A[m][n], matrix B[p][q] and matrix C[m][q]
Step 3: Read m, n, p, q.
Step 4: Now check if the matrix can be multiplied or not, if n is not equal to q matrix can't
be multiplied and an error message is generated.
Step 5: Read A[][] and B[][]
Step 6: Declare variable i=0, k=0 , j=0 and sum=0
Step 7: Repeat Step until i < m
Repeat Step until j < q
Repeat Step until k < p
Set sum= sum + A[i][k] * B[k][j]
Set multiply[i][j] = sum;
Set sum = 0 and k=k+1
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Set j=j+1
Set i=i+1
Step 6: Print C
Step 7: Stop
Flowchart:
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Pseudocode:
BEGIN
DECLARE m, n, p, q
READ m, n, p, q
IF n != p THEN
PRINT "Error: Matrices cannot be multiplied"
ELSE
DECLARE A[m][n], B[p][q], C[m][q]
PRINT "Enter matrix A:"
FOR i = 0 TO m-1 DO
FOR j = 0 TO n-1 DO
READ element
A[i][j] = element
END FOR
END FOR
PRINT "Enter matrix B:"
FOR i = 0 TO p-1 DO
FOR j = 0 TO q-1 DO
READ element
B[i][j] = element
END FOR
END FOR
INITIALIZE C to zero matrix of size (m, q)
DECLARE i = 0, j = 0, k = 0, sum = 0
WHILE i < m DO
j=0
WHILE j < q DO
sum = 0
k=0
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
WHILE k < n DO
sum = sum + A[i][k] * B[k][j]
k=k+1
END WHILE
C[i][j] = sum
j=j+1
END WHILE
i=i+1
END WHILE
PRINT "Result of matrix multiplication:"
FOR i = 0 TO m-1 DO
FOR j = 0 TO q-1 DO
PRINT C[i][j] WITH SPACE
END FOR
PRINT NEWLINE
END FOR
END
Program:
m = int(input("Enter the number of rows of matrix A: "))
n = int(input("Enter the number of columns of matrix A: "))
p = int(input("Enter the number of rows of matrix B: "))
q = int(input("Enter the number of columns of matrix B: "))
if n != p:
print("Error: Matrices cannot be multiplied")
else:
print("Enter matrix A:")
A = []
for i in range(m):
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
row = []
for j in range(n):
element = int(input(f"Enter element A[{i}][{j}]: "))
[Link](element)
[Link](row)
print("Enter matrix B:")
B = []
for i in range(p):
row = []
for j in range(q):
element = int(input(f"Enter element B[{i}][{j}]: "))
[Link](element)
[Link](row)
C = [[0 for _ in range(q)] for _ in range(m)]
i=0
while i < m:
j=0
while j < q:
sum = 0
k=0
while k < n:
sum += A[i][k] * B[k][j]
k += 1
C[i][j] = sum
j += 1
i += 1
print("Result of matrix multiplication:")
for row in C:
for element in row:
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
print(element, end=' ')
print()
Output:
Enter the number of rows of matrix A: 2
Enter the number of columns of matrix A: 2
Enter the number of rows of matrix B: 2
Enter the number of columns of matrix B: 2
Enter matrix A:
Enter element A[0][0]: 1
Enter element A[0][1]: 2
Enter element A[1][0]: 3
Enter element A[1][1]: 4
Enter matrix B:
Enter element B[0][0]: 5
Enter element B[0][1]: 6
Enter element B[1][0]: 7
Enter element B[1][1]: 8
Result of matrix multiplication:
19 22
43 50
Enter the number of rows of matrix A: 2
Enter the number of columns of matrix A: 3
Enter the number of rows of matrix B: 3
Enter the number of columns of matrix B: 2
Enter matrix A:
Enter element A[0][0]: 5
Enter element A[0][1]: 6
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Enter element A[0][2]: 7
Enter element A[1][0]: 8
Enter element A[1][1]: 9
Enter element A[1][2]: 10
Enter matrix B:
Enter element B[0][0]: 1
Enter element B[0][1]: 2
Enter element B[1][0]: 10
Enter element B[1][1]: 11
Enter element B[2][0]: 13
Enter element B[2][1]: 14
Result of matrix multiplication:
156 174
228 255
2.6 Solving a Quadratic Equation
Design an algorithm, flowchart and pseudocode to find the roots of a quadratic equation of
the form ax^2 + bx + c = 0. Solve: x2+2x+5=0, x2−5x+6=0, x2−4x+4=0.
Algorithm:
Step 1: Start
Step 2: Read a, b, c
Step 3: compute disc = b²- 4ас
Step 4: if (disc < 0) then
roots are imaginary
Step 5: else if (disc = 0) then
r=-b+√disc/2a
print r
Step 6: else
r1=-b+√disc / 2a
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
r2=-b-√disc/2a
Step 7: print r1 and r2
Step 8: Stop
Flowchart:
Pseudocode:
BEGIN
DECLARE a, b, c, disc, r, r1, r2
READ a, b, c
disc = b^2 - 4ac
IF disc < 0 THEN
PRINT "roots are imaginary"
ELSE IF disc = 0 THEN
r=-b+√disc/2aPRINT r
ELSE
r1 = (-b + sqrt(disc)) / (2a)
r2 = (-b - sqrt(disc)) / (2a)
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
PRINT r1, r2
END IF
END
Program:
import math
a = int(input("Enter coefficient a: "))
b = int(input("Enter coefficient b: "))
c = int(input("Enter coefficient c: "))
disc = b**2 - 4*a*c
if disc < 0:
print("The roots are imaginary")
elif disc == 0:
r = (-b + [Link](disc)) / (2 * a)
print("The roots are real and equal:",r)
else:
r1 = (-b + [Link](disc)) / (2*a)
r2 = (-b - [Link](disc)) / (2*a)
print("The roots are real and different:", r1, "and", r2)
Output:
Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 5
The roots are imaginary
Enter coefficient a: 1
Enter coefficient b: -5
Enter coefficient c: 6
The roots are real and different: 3.0 and 2.0
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Enter coefficient a: 1
Enter coefficient b: -4
Enter coefficient c: 4
The roots are real and equal: 2.0
2.7 Travel Planner
Develop an algorithm, flowchart and pseudocode that recommends travel destinations
based on the user’s budget and preference (Adventure or Relaxation). If the budget is over
₹1,50,000, recommend the Himalayas (for Adventure) or Paris (for Relaxation). Otherwise,
recommend Goa (for Relaxation) or Waynad (for Adventure).
Algorithm:
Step 1: Start
Step 2: Declare budget, preference, and destination
Step 3: Read budget and value of preference
Step 4: If budget is greater than 150000:
If preference is "adventure":
Set destination to "Himalayas"
Else:
Set destination to "Paris"
Step 5: Else:
If preference is "relaxation":
Set destination to "Goa"
Else:
Set destination to "Waynad"
Step 6: Print the destination
Step 7: Stop
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
Flowchart:
Pseudocode:
BEGIN
DECLARE budget, preference, destination
READ budget
READ preference
IF budget > 150000 THEN
IF preference = "adventure" THEN
destination = "Himalayas"
ELSE
destination = "Paris"
ELSE
IF preference = "relaxation" THEN
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru
destination = "Goa"
ELSE
destination = "Waynad"
PRINT destination
END
Program:
budget = float(input("Enter your budget (in ₹): "))
preference = input("Enter your travel preference (Adventure/Relaxation): ")
if budget > 150000:
if [Link]() == "adventure":
destination = "Himalayas"
else:
destination = "Paris"
else:
if [Link]() == "relaxation":
destination = "Goa"
else:
destination = "Waynad"
print(f"Recommended travel destination: {destination}")
Output:
Enter your budget (in ₹): 1455567
Enter your travel preference (Adventure/Relaxation): ADVENTURE
Recommended travel destination: Himalayas
Enter your budget (in ₹): 140000
Enter your travel preference (Adventure/Relaxation): adventure
Recommended travel destination: Waynad
Mrs. B. Lakshmi, Asst. Professor, Dept. of Computer Applications, VRSEC, Kanuru