Skip to content
Snippets Groups Projects
Select Git revision
  • 1e5d5482af16ab0a5fecf765421a3f014b59e7c8
  • main default protected
2 results

cours_22.md

Blame
  • nb1er_refactored.c 564 B
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <stdbool.h>
    
    // fonction testant si un nombre est 1er
    bool is_prime(int nb);
    
    void main() {
       int nb = 1;
       printf("Entrez un nombre: ");
       scanf("%d",&nb);
    
       if (is_prime(nb)) {
          printf("Le nombre %d est premier\n",nb);
       } else {
          printf("Le nombre %d n'est pas premier\n",nb);
       }
    }
    
    bool is_prime(int nb) {
       bool premier = true;
       for (int div=2;div<=sqrt(nb);div++) {    
          if (nb%div == 0) {
             premier = false;
             break;
          }
       }
       return premier;
    }