4th Set of Exercises
4th Set of Exercises
Institute of Sciences
Departement of science and technology (ST) – 1st Year (LMD)
Course : Computer Science 1
4th Serie of Exercises (Practical Work)
First part: `if- elif ` statements.
1. Ask the user to input a color (red, yellow, or green). Print the corresponding action:
a. Red: Stop
b. Yellow: Slow down
c. Green: Go
2. Write a program that calculates the absolute value of a number using conditional
statements.
3. Write a program that takes two numbers as input and compares them, indicating which
number is larger, smaller, or if they are equal.
4. Write a Python program that asks the user to input two numbers and an operator (+, -,
*, /). Perform the specified operation on the two numbers and print the result.
5. Create a program that checks if a character entered by the user is a vowel or a consonant.
6. Write a Python program that calculates the solutions of a second-degree equation
(quadratic equation) of the form 'ax^2 + bx + c = 0.' The program should ask the user to
input the coefficients 'a,' 'b,' and 'c,' and then determine and display the solutions (roots)
of the equation.
2024/2025
2. Write a program that asks the user for a number between 1 and 30 repeatedly until a
satisfactory response is provided.
3. Write an algorithm that requests an initial number and calculates the sum of integers up
to that number. For example, if the input is 5, the program should calculate:
1 + 2 + 3 + 4 + 5 = 15
Note: Only the result should be displayed, not the breakdown of the calculation.
4. Write a Python program that calculates the factorial of a non-negative integer 'n.' The
program should prompt the user to enter the value of 'n' and then calculate and display
'n!' (n factorial).
5. Ask the user to enter a string and count the number of vowels (a, e, i, o, u) in the string
using a `for` loop.
6. Write a program that calculates and prints the Greatest Common Divisor (GCD) of two
given numbers.
7. Write a python program that determines whether a positive integer N is perfect or not.
A number is considered perfect if it is equal to the sum of its divisors.
For example: 6 = 1 + 2 + 3.
8. Write a program that reverses a user-input string using a `for` loop.
Additional exercises
- Write a program that checks if a given number is positive or negative.
- Write a Python program that asks the user to input their age. Based on the age, print an appropriate
message:
- Write a program that reads the temperature of water and displays its state as follows:
• 'ICE' if the temperature is below 0,
• 'LIQUID' if the temperature is strictly above 0 and below 100
• 'VAPOR' if the temperature is above 100."
2024/2025
- Write a program that asks the user to enter two numbers. The program should then determine and
display whether the product of these two numbers is positive or negative without calculating the
actual product.
- Write a program that calculates the sum of all even numbers until 20, then the sum of all odd
numbers until20.
- Create a program that takes a number from the user (should be > 1) and then prints a count down
from that number to 1 using a `while` loop.
- Create a program that asks the user to enter two numbers and an operator (+, -, *, /) and performs
the corresponding calculation using a `while` loop to continue calculating if desired.
- Write an algorithm that reads an integer and displays it in reverse. The algorithm should construct
and then display the reversed integer, rather than displaying each digit individually. For example,
if the input is 12345, the program should display the integer 54321.
2024/2025