aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/td/2- exemples/boucle.c
blob: b582080a5d4571813294ab3b8b5317dcd9b694a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <assert.h>

int somme_carres_while(int m, int n){
	int s = 0, i = m;
	while (i <= n){
		s += i*i;
		i++;
	}
	return s;
}

int somme_carres_for(int m, int n){
	int s = 0;
	for (int i = m; i <= n; i++){
		s += i*i;
	}
	return s;
}

int main(){
	assert(somme_carres_while(0, 5) == somme_carres_for(0, 5));
	assert(somme_carres_while(3, 5) == somme_carres_for(3, 5));
	assert(somme_carres_while(15, 25) == somme_carres_for(15, 25));
	return 0;
}