0% found this document useful (0 votes)
15 views2 pages

Introduction to Basic Algorithms

The document contains various algorithm exercises and solutions related to basic programming concepts, including conditional statements and mathematical operations. It provides examples of algorithms for calculating values such as BMI, determining maximum values, and evaluating conditions based on user inputs. Each exercise is structured with variable declarations, input prompts, and output statements.

Uploaded by

oualiddrici0
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)
15 views2 pages

Introduction to Basic Algorithms

The document contains various algorithm exercises and solutions related to basic programming concepts, including conditional statements and mathematical operations. It provides examples of algorithms for calculating values such as BMI, determining maximum values, and evaluating conditions based on user inputs. Each exercise is structured with variable declarations, input prompts, and output statements.

Uploaded by

oualiddrici0
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

Introduction to algorithms Abdelhamid Mehri – Constantine 2 University

1st year Common Core – MI, NTIC Faculty

Do not consider possible user error cases in this series


Solution of Exercise 2.01:
Algo Exo2_1_a Algo Exo2_1_b The mod operation gives the
Var Var remainder of the division
A, B, C : Integer N, P : Integer (13 mod 5 = 3)
Begin Begin
Read (A,B) For A=3 and B=6, C= 15 Read (N) For N = 6, P = 31
If A < 5 or B < 8 Then For A=3 and B=10, C= 23 If N mod 2 = 0 Then For N = 9, P = 28
C A+B*2 N N+4
Else For A=5 and B=10, C= 20 EndIf
C A*2+B P 1+N*3
EndIf Write(P)
Write (C) End
End
Algo Exo2_1_c Algo Exo2_1_d
Var Var
N, R : Integer A, B, C, D : Real
Begin Begin
Read(N) For N = 5, R = 1 Read(A,B,C) For A=2, B=15 and C=9, D = 15.0
If N < 0 Then For N = -13, R = D A For A=0, B=-3 and C=12, D =12.0
0
R 0 If B > D Then
Else For N = 21, R = 2 D B For A=27,B=10 and C=19, D=27.0
If N < 10 Then EndIf
R 1 If C > D Then
Else D C - What does this algorithm do?
R 2 EndIf
EndIf Write (D) Displays the maximum of
EndIf End three real numbers entered.
Write(R)
End
Solution of Exercise 2.04 :
Algo Exo2_4_3 Algo Exo2_4_1
Var Var
P, T, BMI: Real P, T, BMI : Real
Begin Begin For P=60 and T=1.60, BMI= 23.4375
Write ("Please enter your weight in Write ("Please enter your weight in
Normal range
kg, then your height in meters ") kg, then your height in meters ")
Read (P, T) Read (P, T)
BMI P / (T*T) BMI P / (T*T)
For P=75 and T=1.65, BMI= 27.5482
Write(BMI) Write(BMI)
If BMI < 18.5 Then If BMI < 18.5 Then Overweight
Write ("Thinness") Write ("Thinness")
EndIf Else
If BMI ≥ 18.5 and BMI ≤ 25 Then If BMI ≤ 25 Then For P=55 and T=1.75, BMI= 17.9592
Write ("Normal range") Write ("Normal range") Thinness
EndIf Else
If BMI > 25 Then Write ("Overweight")
Write ("Overweight") EndIf
EndIf EndIf
End End

1/2
Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
1st year Common Core – MI, NTIC Faculty

Solution of Exercise 2.05 : Exercise 2.06 :


Algo Exo2_5 Algo Exo2_6_a Algo Exo2_6_b
Var Var Var
X1, X2, X3, X4, X5, Max : Real Nb : Integer A, B, C : Integer
Begin Begin Begin
Read(X1, X2, X3, X4, X5) Read(Nb) Read(A,B,C)
Max X1 If Nb mod 2 = 0 Then Si A mod 5 = 0 Then
If X2>Max Then Write ("Even") Write (A)
Max X2 Else EndIf
EndIf Write ("Odd") If B mod 5 = 0 Then
If X3>Max Then EndIf Write (B)
Max X3 End EndIf
EndIf If C mod 5 = 0 Then
If X4>Max Then Write (C)
Max X4 EndIf
EndIf End
If X5>Max Then
Max X5
EndIf
Write (Max)
End

Exercise 2.13 :

Algo Ex_2_13_a Algo Ex_2_13_b Algo Ex_2_13_c Algo Ex_2_13_d Algo Ex_2_13_e
Var Var Var Var Var
A, B, C : Real A, B, C : Real A, B, C : Real A, B, C : Real A, B, C : Real
Begin Begin Begin Begin Begin
Read (A, B) Read (A, B) Read (A, B) Read (A, B) Read (A, B)
C A / B*B C A / (B*B) C A / B*B C A / B*B C A / B*B
If C < 10 Then If C ≤ 10 Then If C < 10 Then If C > 20 Then If C ≤ 20 Then
Write (A) Write (A) Write (A) Write (B) Write (C)
EndIf EndIf Else Else Else
If C≥10 et C≤20Then If C>10 et C≤20Then If C ≤ 20 Then If C < 10 Then If C < 10 Then
Write (C) Write (C) Write (C) Write (A) Write (A)
EndIf EndIf Else Else Else
If C > 20 Then If C > 20 Then Write (B) Write (C) Write (B)
Write (B) Write (B) EndIf EndIf EndIf
EndIf EndIf EndIf EndIf EndIf
End End End End End

2/2

You might also like