aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/td/2- exemples/boucle_complet.c
diff options
context:
space:
mode:
Diffstat (limited to 'semestre 2/informatique/td/2- exemples/boucle_complet.c')
-rw-r--r--semestre 2/informatique/td/2- exemples/boucle_complet.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/semestre 2/informatique/td/2- exemples/boucle_complet.c b/semestre 2/informatique/td/2- exemples/boucle_complet.c
new file mode 100644
index 0000000..9cae542
--- /dev/null
+++ b/semestre 2/informatique/td/2- exemples/boucle_complet.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#define MAX 20
+
+int premier(int n){
+ if (n < 2) return 0;
+ for (int i = 2; i < n;i++){
+ if (n%i == 0) return 0; // est une boucle avec sortie anticipée
+ }
+ return 1;
+}
+
+void afficheNombresPremiers(int max){
+ for (int i = 2; i <= max; i++){
+ if (premier(i)) printf("%d\n", i);
+ }
+}
+
+int main(){
+ printf("Liste des nombres premiers <= %d\n", MAX);
+ afficheNombresPremiers(MAX);
+ return 0;
+}