Practice
Practice
sum,
product, and difference.
Hint: subtract the second number from the first one
==========================================================
Example 1:
Sample Input:
4
5
Sample Output:
Sum = 9
Product = 20
Difference = -1
Write the Python code of a program that reads two numbers from the user. The program should then
print "First is greater" if the first number is greater, "Second is greater" if the second number is
greater, and "The numbers are equal" otherwise.
==========================================================
Sample Input 1:
7
3
Sample Output 1:
First is greater
==========================================================
Write the Python code of a program that reads two numbers, subtracts the smaller number from the
larger one, and prints the result.
Hint: First, we may check which number is greater
==========================================================
Sample Input 1:
-40
-4
Sample Output 1:
36
Explanation: -4 > -40 so -4 - (-40) = -4 + 40 = 36
==========================================================
Write the Python code of a program that reads a number, and prints "The number is even" or "The
number is odd", depending on whether the number is even or odd.
hint(1): we may use the modulus (%) operator to check for even or odd
hint(2): we can consider the number to be an integer
==========================================================
Sample Input 1:
7
Sample Output 1:
The number is odd
==========================================================
Sample Input 2:
10
Sample Output 2:
The number is even
Write the Python code of a program that reads an integer as input from the user, and prints the integer
if it is a multiple of 2 OR 5 and prints "Not a multiple of 2 OR 5" otherwise.
For example, 2, 4, 5, 6, 8, 10, 12, 14, 15, 16, 18, 20, 22 … i.e. this includes multiples of 2 only,
multiples of 5 only and multiples of 2 and 5 both.
==========================================================
hint(1): we may use the modulus (%) operator for checking the divisibility
hint(2): we can consider the number to be an integer
==========================================================
Sample Input 1:
5
Sample Output 1:
5
Write the Python code of a program that reads an integer, and prints the integer if it is a multiple of 2
AND 5 and prints "Not multiple of 2 and 5 both" otherwise.
==========================================================
For example, 10, 20, 30, 40, 50 … i.e. this only includes numbers which are multiples of both 2 and
5.
==========================================================
hint(1): we may use the modulus (%) operator for checking the divisibility
hint(2): we can consider the number to be an integer
==========================================================
Sample Input 1:
30
Sample Output 1:
30
Write a Python program that takes an hour from the user as input and tells it is time for which meal.
• The user will input the number in a 24-hour format. So, 14 means 2 pm, 3 means 3 am, 18 means 6
pm, etc.
• Valid inputs are 0 to 23. Inputs less than 0 or more than 23 are invalid in 24-hour clock.
• Assume, input will be whole numbers. For example, 3.5 will NOT be given as input.
==========================================================
Input range: Message to be printed
4 to 6: Breakfast
12 to 13: Lunch
16 to 17: Snacks
19 to 20: Dinner
For all other valid inputs, say "Patience is a virtue"
For all other invalid inputs, say "Wrong time"
==========================================================
For example,
If the user enters 4, your program should print the message "Breakfast".
If the user enters 5, your program should print the message "Breakfast".
If the user enters 6, your program should print the message "Breakfast".
If the user enters 0, your program should print the message "Patience is a virtue".
If the user enters 1, your program should print the message "Patience is a virtue".
If the user enters 18, your program should print the message "Patience is a virtue".
If the user enters 23, your program should print the message "Patience is a virtue".
If the user enters 24, your program should print the message "Wrong Time".
If the user enters -1, your program should print the message "Wrong Time".
If the user enters 27, your program should print the message "Wrong time".
==========================================================
Hints: You can use nested conditionals (if-else) or chained conditions (if-elif-else) to solve this
problem.
Write the Python code of a program that displays all the odd numbers between 10 and 50
(inclusive).
Output: 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Write a Python code of a program that asks the user to enter ten numbers and then display the
total and the average of ONLY the odd numbers among those ten numbers.
Write a Python program that takes a number as input from the user and tells if it is a perfect number
or not.
Perfect Number: An integer number is said to be a perfect number if its factors, including 1 but not
the number itself, sum to the number.
===============================================================
Sample Input 1: 6
Sample Output 2: 6 is a perfect number
Explanation: 6 has 4 divisors: 1, 2, 3, and 6. If we add all factors except 6 itself, 1 + 2 + 3 = 6. The
sum of the factors excluding the number itself sum up to the number therefore "6 is a perfect
number" is printed.
===============================================================
Sample Input 2: 28
Sample Output 2: 28 is a perfect number
Explanation: 28 has the divisors: 1, 2, 4, 7, 14, and 28. If we add all factors except itself, we get 28,
1 + 2 + 4 + 7 + 14 = 28.
===============================================================
Sample Input 3: 33
Sample Output 3: 33 is not a perfect number
Explanation: 33 has 4 divisors: 1, 3, 11, and 33. If we add all factors except itself, 15 = 1 + 3 + 11.
The sum is not equal to the number, therefore 33 is not a perfect number.
Task 15
Write a Python program that asks the user for one number and tells if it is a prime number or not.
Prime Number: If a number has only two divisors, (1 and itself), then it is a prime number. If it is
divisible by more numbers, then it is not a prime.
Hint: You may use the code for counting divisors from Task 14.
===============================================================
Sample Input 1: 11
Sample Output 1: 11 is a prime number
Explanation: 11 has only 2 divisors: 1 and 11.
===============================================================
Sample Input 2: 6
Sample Output 2: 6 is not a prime number
Explanation: 6 has 4 divisors: 1, 2, 3 and 6.
===========================================
Write a Python program that asks the user for a quantity, then takes that many numbers as input and
prints the maximum, minimum and average of those numbers.
[Please note that you CANNOT use max, min built-in functions]
[Also, you DO NOT need to use lists for this task]
===============================================================
Example: If the user enters 5 as an input for quantity and the enters the 5 numbers, 10, 4, -1, -100,
and 1. The output of your program should be: “Maximum 10”, “Minimum -100”, “Average is -17.2”
as shown below.
Input: 5
10
4
-1
-100
1
Output: Maximum 10 Minimum -100 Average is -17.2
Explanation: Average calculation: (10 + 4 + (-1) + (-100) + 1)/5 = -86/5 = -17.2
=============================================
Write a python program that prints a square of size N using + where N will be given as input as
illustrated in the examples below.
Hint: You may need to use nested loops to solve this problem.
===============================================================
Sample Input 1:
5
Sample Output 1: +++++ +++++ +++++ +++++ +++++
Explanation: Here, we may see it as 5 rows and 5 columns, where in each horizontal row/line, 5 '+'
is printed next to each other and we have a total of 5 lines.
=================================================