Department of Computer Science and Engineering
PES University, Bangalore, India
Lecture Notes
Python for Computational Problem Solving
UE23CS151A
Lecture #22, #25, #26
Problem Solving using Control Structures
Practice Session + Revision
By,
Prof. Sindhu R Pai
Anchor, PCPS - 2023
Assistant Professor
Dept. of CSE, PESU
Verified by,
PCPS Team - 2023
Many Thanks to
Dr. Shylaja S S (Director, CCBD and CDSAML Research Centers, Former
Chairperson, CSE, PES University)
Prof. Chitra G M(Asst. Prof, Dept. of CSE, PCPS Anchor – 2022)
Aug – Dec, 2023
Practice Programs
Try solving these problem statements. Solutions for a few are available in
this link:
[Link]
K/view?usp=drive_link
1. Program to print from 1 to n in separate lines.
2. Program to find the sum of all numbers from 1 to n.
3. Input a string from the user. Print the Unicode of every character in the string in a
separate line.
4. Program to print all odd numbers from 1 to n in separate lines.
5. Program to print all numbers divisible by both 3 and 5 between 1 and n in separate
lines.
6. Program to print all numbers between 1 and n which are ending with 9 in separate
lines.
7. Program to print all numbers from 1 to 10 except 5.
8. WAP to take first name, middle name and last name as input from the user in different
lines and print it on the terminal as one name with spaces.
9. Classify the number entered by the user as even number or odd number. Till user says
stop, he must be able to enter the number for checking its oddness or evenness.
10. Generate n random integers between 1 and 1000.
11. WAP to print all Armstrong numbers from 1 to n. If not available, display proper
message to the user.
Armstrong number is a number, if the sum of its own digits raised to the power
number of digits gives the number itself. For example, 0, 1, 153, 370, 371, 407 are
three-digit Armstrong numbers and, 1634, 8208, 9474 are four-digit Armstrong
numbers and there are many more.
12. Given a number, find whether the number is an Armstrong number or not
Department of CSE, PESU 2
Aug – Dec, 2023
13. Display whether a given number is perfect number or not.
A perfect number is a positive integer that is equal to the sum of its positive
divisors, excluding the number itself.
14. Program to swap first and last digits of a given number and print the new number.
15. Program to check whether a given number is palindrome or not for any positive
integer. Display appropriate message if the number is –ve.
16. Generate all three digit palindrome numbers.
17. Program to find all Factors of a given number.
18. Program to print all Strong numbers between 1 to n.
Strong number is a special number whose sum of the factorial of digits is equal to
the original number. For Example: 145 is strong number. Since, 1! + 4! + 5!
19. Program to display the hexadecimal and octal version of a given number.
20. Program to enter a number and print its reverse.
21. WAP to print multiplication table of any given number.
22. Given 4 sides and two diagonals of a quadrilateral, classify the quadrilateral as square,
rhombus, rectangle, parallelogram, kite, just quadrilateral.
Requirements: Read four sides and two diagonals. Store in different variables and
convert them to integer values
Conditions:
a. If all sides equal and diagonals equal, display "it is a square"
b. Else if two opposite sides equal and diagonals equal, display "it is a rectangle"
c. else if …
23. Given 3 points A(x1, y1), B(x2, y2) and C(x3, y3), find whether they are collinear. If no,
find the centroid.
Logic: Slope of AB = Slope of BC = Slope of AC
Centroid = ((x1+x2+x3)/3 , (y1+y2+y3)/3)
24. For a given n, display as below. Example n = 4
a
ab
abc
abcd
Department of CSE, PESU 3
Aug – Dec, 2023
25. Given n, display the following (say 4)
i)1 0 0 0
0100
0010
0001
ii) 1 2 3 4
5678
9 10 11 12
13 14 15 16
26. Given three sides of a triangle, check whether the triangle can be formed or not. If
triangle can be formed, find the type of triangle.
27. Print the lengths of all words in a given string. After the first length, put a decimal point.
Input: May I have a large container of coffee? Expected Output: 3.1415926
28. Program to find the diameter, area and circumference of circle by taking radius as the
input from the user
29. Program to convert the given Decimal number into Binary
30. Program to display sum of odd numbers and even numbers separately between 1 to n.
Few more listed below:
1. Display Snake - Ladder Game chart
2. Program to calculate the LCM of two numbers.
3. Sumedh offers a discount of 10% on the cost price of the TV unit. TV unit price is
35000 . What is the selling price of a TV unit?
4. A shop will give a discount of 10% if the cost of the quantity purchased is more
than 1000. Ask the user for quantity. Suppose, one unit will cost 100. Judge and print
total cost for the user.
i. quantity = int(input("Enter the number of quantities"))
ii. if (quantity*100 > 1000):
1. print ("Cost is",((quantity*100)-(.1*quantity*100)))
iii. else:
1. print ("Cost is",quantity*100)
Department of CSE, PESU 4
Aug – Dec, 2023
5. Count the number of factors for a given number and the sum of factors for a given
number.
6. Display the squares of all numbers and the square root of all numbers from 1 to n.
7. Program to display the nth Fibonacci number.
8. Program to display a number from fibonacci series which is just greater than n
9. Program to find the product of three sequential terms from 1 to n.
Hint: If n = 8,
Output: 6, 120,56
10. Display all the numbers from 1 to n except n//2
-END-
Department of CSE, PESU 5