Algorithm Exercises and Solutions
Algorithm Exercises and Solutions
Correction exercise 1:
Algorithm absolute_value;
Var
X: real numbers;
Debut
Write('enter a number: ');
Read(X);
If X > 0 then
Write('the absolute value of X=',X);
Otherwise
Write('the absolute value of X=', X);
Finish
end
Exercise 2:
Write an algorithm that displays the absolute value of the difference between
two real numbers entered via the keyboard.
|x-y| = x - y if x > y
|x-y| = -(x - y) if x < y
Correction exercise 2:
Algorithm absolute_value;
Var
x,y : real numbers;
Debut
Write('enter x:');
Read(x);
Write('enter y:');
Read
If x > y then
Write('the absolute value of x-y=', x-y);
Otherwise
Write('the absolute value of x-y =', y-x);
Finish
end
Exercise 3:
We want to write an algorithm that displays the corresponding day for a
number ranging from 1 to 7, entered via keyboard.
Solve this problem with two methods: (if nested, primitive case).
Correction 1 exercise 3:
Algorithm display_day;
Var
whole day
Debut
Write('enter a number from 1 to 7: ');
Read(day);
If day=1 then
Write('Monday');
Otherwise If day=2 then
Write('Tuesday');
Else If day = 3 then
Write('Wednesday');
Else If day=4 then
Write('Thursday');
Else if day=5 then
write('Friday');
Else if day=6 then
Write('Saturday');
Otherwise, If day=2 then
write('Sunday');
Otherwise
Correction 2 exercise 3:
Algorithm display_day;
Var
day:whole;
Debut
Write('enter a number from 1 to 7:');
Read(day);
Case day
1: Write('MONDAY');
2: Write('TUESDAY');
3: Write('WEDNESDAY');
4: Write('THURSDAY ');
5: Write('FRIDAY')
6: Write('SATURDAY');
7: Write('SUNDAY');
Otherwise
Write('you must choose a number between 1 and 7 !!!!')
FinCas
end
Exercise 4:
Write an algorithm that allows you to input two integers x, y and display them.
on the screen in ascending order.
Correction exercise 4:
Algorithm two_number_sort;
Var
x,y: real numbers;
Debut
Write('enter x: ');
Read(x);
Write('enter y: ');
Read
If x > y then
Write(y, ' ', x);
Otherwise
Write(y, ' ', x);
Finish
end
Exercise 5:
Write an algorithm that tests whether a year is a leap year or not.
N.B.
A year is a leap year if it is divisible by 4 and not by 100 or if it is
divisible by 400.
Correction exercise 5 :
Algorithm leap_year;
Var
reals
Debut
Write('enter the year: ');
Read(year);
If ((year mod 4 = 0 and year mod 100 <> 0) or year mod 400 = 0) then
Write('the year you entered is a leap year.')
Otherwise
Write('the year you entered is not a leap year.')
Finish
end
Exercise 6:
Write an algorithm to solve a second degree equation:
ax² + bx + c = 0.
Correction exercise 6:
Algorithm calculate_permutation;
Var
A, b, c, d: real numbers;
Debut
Write('enter the coefficient a : ');
Read(a);
Write('enter the coefficient b: ');
Read(b);
Write('enter the coefficient c: ');
Read(c);
If a=0 then
If b=0 then
If c=0 then
Write(' the solution is : S = R');
otherwise
Write('the equation has no solution');
Finish
otherwise
Write('the solution is: S = ', -c/b);
Finish
otherwise
D<-- b*b-4+a*c ;
If d=0 then
Write('the solution is: S = ', -b/(2*a));
Otherwise, if d > 0 then
Write('the equation has two solutions: S1= '
(-b - √(2*a), 'and S2 = ', (-b + √(2*a));
Otherwise
Writing('the equation has no solution
in R ') ;
Finsi
Finish
Finish
end
Exercise 7:
A bookstore decides to offer discounts on subscription prices for a magazine.
scientific according to the following menu:
Former subscriber: -15%
Student: -20%
New subscriber: 00%
Foreign: +25%
The subscription price calculation is based on the standard subscription rate.
(TN) and the quality of the subscriber (Q). (only one quality is accepted per subscriber).
Write an algorithm to calculate the price to pay.
Correction exercise 7:
Algorithm library;
Var
Q: integer;
TN, TR, R, RAP: real;
Debut
Write('Enter the normal rate: ');
Read (TN);
Write('--------------- MENU -----------------------');
Write('----------Former Subscriber------------------ 1') ;
Write('-----------------Student------------------- 2');
Write('-----------------New subscriber--------------- 3');
Write('-------------------Stranger---------------------- 4');
Write('Enter the quality of the subscriber?') ;
Read(Q);
Cas Q
1 : TR <-- -0.15 ;
2 : TR <-- -0.20 ;
3 : TR <-- -0.00 ;
4 : TR <-- +0.25 ;
Farms
If (Q<1) or (Q>4) then
Write('Choice error');
Otherwise
R = TN * TR;
PAR <-- TN+R ;
Write('The price to pay is:', PAP);
Finish
End
Exercise 8:
Correction exercise 8:
Calculation algorithm;
Var
X, Y, Z, R: real numbers;
Choice: character;
Debut
Write('Enter the first number: ');
Read(X);
Write('Enter the second number: ');
Read(Y);
Write('Enter the third number: ');
Read(Z);
Write('--------------- MENU -----------------------');
Write('------------------Sum------------------- 1');
Write('----------------- Product-------------------- 2');
Write('------------------Average----------------- 3');
Write('-----------------Minimum------------------ 4');
Write('-----------------Maximum------------------ 5');
Write('Enter your choice?') ;
Read(choice);
Case choice
1 : R <-- X+Y+Z;
2 : R <-- X*Y*Z;
3 : R <-- (X+Y+Z)/3;
If X<Y Then
R ß X ;
Otherwise
R is assigned the value of Y;
Finish
If Z < R Then
R <-- Z ;
finish
If X>Y then
R <-- X;
Otherwise
R <-- Y ;
Finish
If Z > R Then
R <-- Z ;
finish
Farms
If (choice >= 1) and (choice <= 5) then
Write('The result is:', R);
Otherwise
Write('your choice is bad ... !') ;
Finish
Estates
If (Q<1) or (Q>4) then
Write('Choice error');
Otherwise
R <-- TN * TR ;
PAR <-- TN+R ;
Write('The price to pay is:', PAP);
Finish
End
Exercise 9:
Write an algorithm that compares two dates represented by three integers each.
Correction exercise 9:
Algorithm compare_date;
Var
j1,m1,a1,j2,m2,a3: reals;
Debut
Write('************date 1 ***********');
Write('day: ');
Read(j1);
Write('Month: ');
Read(m1);
Write('Year: ');
Read(a1);
Write('************date 2 ***********');
Write('day: ');
Read(j2);
Write('Month: ');
Read(m2) ;
Write('Year: ');
Read(a2);
If a1 > a2 then
Write ('date 1 is greater than date 2');
Otherwise, if a1 < a2 then
Write('date 2 is greater than date 1')
;
Otherwise, if m1 > m2 then
Write ('the date 1 is greater
what the date 2 ') ;
Otherwise, if m1 < m2 then
Write('the date 2 is more
greater than date 1') ;
Otherwise, if j1 > j2 then
Write (
date 1 is greater than date 2
Otherwise, if j1 < j2 then
Write('the
date 2 is greater than date 1
Finish
Finish
Finish
Finish
finish
end