0% found this document useful (0 votes)
30 views7 pages

Python Algorithms for Basic Programming Exercises

This document contains 15 programming exercises in Python along with their solutions. The exercises focus on concepts such as loops, if/else conditions, mathematical functions, and the manipulation of strings and numbers. The document presents each exercise in a structured manner with the statement, the algorithm, and the corresponding Python code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Python Algorithms for Basic Programming Exercises

This document contains 15 programming exercises in Python along with their solutions. The exercises focus on concepts such as loops, if/else conditions, mathematical functions, and the manipulation of strings and numbers. The document presents each exercise in a structured manner with the statement, the algorithm, and the corresponding Python code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SSB Moknine High School

Mr: Karchoud Radhouane 2 SI

Series No. 1

Exercise No. 1:
Write an algorithm and its translation in Python that allows entering two integers a and b (formed by two
to display a four-digit integer r by interleaving a between the two digits of b.
Example: a=54 and b=36 so r=3546

Algorithm exercise1
Start
TDO
Display ("Enter a two-digit integer:")
Read (n1)
Subject Type
Display ("Enter another two-digit integer:")
Read (n2) n1 Enter
a n1 // 10 n2 Enter
b n1 % 10 a Enter
n a * 1000 + n2 * 10 + b b Enter
Display ("the final result = ", n)
End
n1 = int(input("Enter a two-digit integer:"))
n2 = int(input("Enter another two-digit integer:"))
a = n1 // 10
b = n1 % 10
n = a * 1000 + n2 * 10 + b
print('four-digit integer = ', n)

Exercise No. 2:
For each of the propositions below, check the correct answer:
The correct syntax is:

Exercise No. 3:
Write a program that allows you to enter a number and then determine whether it belongs to an interval.
given, knowing that the endpoints of the interval are fixed by the user.
Solution :
a = int(input("enter [a :"))
b = int(input("enter b]:"))
val = int(input("enter a value:"))
if a <= val <= b:
print(val, "belongs to the interval [", a, ", ", b, "]")
else:
print(val, "does not belong to the interval [", a, ", ", b, "]")

Exercise No. 4:
Write a program that asks the user for two numbers and then informs them if their product is
negative or positive. However, be careful not to calculate the product of the two numbers.
Solution :
a = int(input("enter a : "))
b = int(input("enter b: "))
if a > 0 and b > 0 or a < 0 and b < 0:
the product is positive
else:
the product is negative

Exercise No. 5:
Write a program that calculates the amount of overtime for an employee.
knowing the unit price of an hour, according to the following scale:
The first 39 hours without extra charge,
From the 40th to the 44th hour are increased by 50%,
From the 45th to the 49th hour are increased by 75%.
From the 50th hour or more, they are increased by 100%.
Solution :
nbheures = int(input("saisir le nombre d heures"))
price = float(input("enter the unit price of one hour"))
montant = 0
if nbheures <= 39:
montant = 0
elif hours < 45:
amount = (number of hours - 39) * (price * 1.5)
elif hours < 50:
montant = (5*prix*1.5)+(nbheures-44)*(prix*1.75)
else:
montant = (5*prix*1.5)+(5*prix*1.75)+(nbheures-49)*(prix*2)
the amount of overtime is:

Exercise No. 6:
Write a program that will read the hour and minutes from the keyboard, and it will display what time it will be.
a minute later.
For example, if the user types 21 then 32, the program must respond: "In one minute, it will be 21"
hour(s) 33".
Note: It is assumed that the user enters a valid time. Therefore, there is no need to check it.
Solution :
hour = int(input("enter the hour: "))
minutes = int(input("enter the minutes: "))
if hour < 23 :
if minutes == 59:
hour += 1
minutes=0
else :
minutes+=1
else :
if minutes==59 :
heure=0
minutes=0
else:
minutes+=1
after one minute the time is

Exercise No. 7:
Write a program that displays the corresponding mention based on a grade?
Solution :
note=float(input("enter a grade: "))
if note < 10:
not admitted
elif note < 12 :
passable
elif note<14 :
pretty good
elif note<16 :
good
elif note <= 20:
very well
else :
enter a valid grade

Exercise No. 8:
Write an algorithm that asks the user for a child's age. Then, it informs them of their
category :
Chicken from 6 to 7 years old
"Pupil" from 8 to 9 years old
"Minime" from 10 to 11 years old
'Cadet' after 12 years
Solution :
Solution 1

Algorithm child_category :
Start
Display ("Enter the age: ");
Read(age);
IF age < 6 THEN
Display ("Enter an age greater than or equal to 6")
IF age <=7 THEN
Display ("Chick")
IF age <= 9 THEN
Display ('Pupil')
ELSE IF age <= 11 THEN
Display ("Minimum")
SINON
Display ("Cadet")
FIN IF
End

Solution 2
Algorithm category_child :
Start
Display("Enter the age: ");
Read(age);
IF age < 6 THEN
Display ("Enter an age greater than or equal to 6")
IF age >= 6 AND age <= 7 THEN
Display ("Chick")
IF age >=8 AND age <=9 THEN
Show (Pupil)
IF age >=10 AND age <=11 THEN
Display ("Minimal")
ALTERNATIVELY
Display ("Cadet")
FIN YES
End

Exercise No. 9:
Write an algorithm that, given a number between 1 and 7, displays the corresponding day?
Solution :
Algorithm week_day :
Start
Display ("Enter a day:");
Read(nb) ;
IF day=1 THEN
Display ("Monday")
ELSE IF day=2 THEN
Display ("Tuesday")
IF day=3 THEN
Display ("Wednesday")
IF day=4 THEN
Display ("Thursday")
IF day=5 THEN
Display ("Friday")
IF day=6 THEN
Show ("Saturday")
SINON SI jour=7 ALORS
Display ("Sunday")
IF NOT
Display ("invalid day")
FINE IF
End
Solution 2:
Algorithm week_days:
Start
Display ('Enter a value: ');
Read (day);
ACCORDING to the day
1: Display ("Monday")
2 : Display ("Tuesday")
3 : Display ("Wednesday")
4: Display ("Thursday")
5 : Show ("Friday")
6 : Display ("Saturday")
7: Show ("Dianche")
ELSE : Display ("Invalid day")
End ACCORDING TO

End

Exercise No. 10:


Write an algorithm to swap the values of two variables A and B, regardless of
their prior content.
Solution :
Exchange algorithm
Start
Write('Enter the value of A: ')
Read
Write("Enter the value of B: ")
Read(b)
c <- a
a<- b
b <- c
Write("a = ", a, " b=" , b)
End

Exercise No. 11:


Write an algorithm that calculates and displays the gross salary of a worker knowing the
number of hours and the hourly rate?
Solution :
Algorithm gross_salary:
Start
Write("Enter the hourly rate: ")
Read (rate)
Write("Enter the number of hours: ")
Read(nb_hours)
salaire <- tarif * nb_heures
Write("the gross salary = ", salary)
End

Exercise No. 12:


Write the algorithm that allows entering the parameters of a quadratic equation and calculating
the discriminant delta.
Solution :
Discriminant algorithm
Start
Write("Enter a : ")
Read
Write("Enter b: ")
Read(b)
Write("Enter c: ")
Read(c)
delta = (b * b) - 4 * a * c
Write("delta = ", delta)
End

Exercise No. 13:


Write a program that takes a date divided into its components (D, M, Y) and displays the date of
The next day. Take into account the case where the entered date is the last day of the month or that of the last.
day of the year
Note: take 28 as the number of days in the month of February.
Solution :
A = int(input("enter a year:"))
M = int(input("enter the month: "))
J = int(input("enter the day: "))
if M > 12 or J > 31:
the date is invalid
else:
if M == 12:
if J < 31:
J += 1
else:
J=1
M=1
A += 1
elif M == 2:
if J > 28:
invalid day
elif J == 28:
J=1
M += 1
else:
J += 1
else:
if M == 1 or M == 3 or M == 5 or M == 7 or M == 8 or M == 10:
if J == 31:
J=1
M += 1
else:
J += 1
else:
if J == 30:
J=1
M += 1
else:
J += 1
the date of tomorrow is

Exercise No. 14:


To calculate the averages of his students, a teacher calculates two averages; the average
arithmetic and the average of the lowest and highest of three grades. He will choose by the
based on the better of the two averages calculated.
Write a program that inputs the three grades of a student and displays the final average granted.
Example:
If a student's three grades are: 12, 8, 14 then:
Arithmetic mean = (12 + 8 + 14) / 3 = 34 / 3 = 11.34
Average of the worst and the best: (14+8)/2=22/2=11
The teacher will choose the first average.
Solution :
N1 = float(input("enter the first grade:"))
N2 = float(input("enter the second grade:"))
N3 = float(input("enter the third grade:"))
M1 = (N1+N2+N3)/3
if N1 <= N2 <= N3 or N3 <= N2 <= N1:
M2 = (N1 + N3) / 2
elif N2 <= N1 <= N3 or N3 <= N1 <= N2:
M2 = (N3+N2)/2
else:
M2 = (N1 + N2) / 2

if M1 > M2:
the best grade is:
else:
the best grade is : M2

Exercise No. 15:

You might also like