Back

Correction exercices les tableaux en PHP : Série 01

Correction exercices les tableaux en PHP : Série 01

  1. Exercice 01

    1. Énoncé
    2. Solution



  2. Exercice 02

    1. Énoncé
    2. Solution
      • <!doctype html>
        <html lang="fr">
          <head>
            <!-- Required meta tags -->
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
        
        
            <title>Exercice2</title>
        
          </head>
          <body class="container">
         
          <?php
        $notes_apprenants=array(
        'Mouhamed'=>16,
        'Ahmed'=>14,
        'Rafika'=>13,
        'Aicha'=>15,
        'Samir'=>13,
        'Samar'=>13,
        'Rafik'=>10,
        'Samiha'=>9,
        'Fourat'=>7,
        'Sami'=>7,
        'Noura'=>14
        );
        $table="<table class='table table-bordered'><thead><tr><th>Nom</th><th>Note</th></tr></thead><tbody>";
        echo ("<h2>TABLEAU ASSOCIATIF</h2>");
        echo $table;
        
        foreach ($notes_apprenants as $prenom =>$note)
        { 
        echo ("<tr><td>".$prenom."</td>"."<td>".$note."</td></tr>");
        };
        echo("</tbody></table>");
        asort($notes_apprenants);
        echo ("<h2>TABLEAU CROISSANTE</h2>");
        echo $table;
        
        foreach ($notes_apprenants as $prenom =>$note)
        { 
        echo ("<tr><td>".$prenom."</td>"."<td>".$note."</td></tr>");
        }
        echo("</tbody></table>");
        arsort($notes_apprenants);
        echo ("<h2>TABLEAU DECROISSANTE</h2>");
        echo $table;
        
        foreach ($notes_apprenants as $prenom =>$note)
        { 
        echo ("<tr><td>".$prenom."</td>"."<td>".$note."</td></tr>");
        }
        echo("</tbody></table>");
        ?>
        
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
        
          </body>
          </html>
  3. Exercice 03

    1. Énoncé
    2. Solution
      • <!doctype html>
        <html lang="en">
          <head>
            <!-- Required meta tags -->
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
        
        
            <title>expl</title>
        
          </head>
          <body class="container">
         
          <?php
        $notes_apprenants=array(
        'Mouhamed'=>16,
        'Ahmed'=>14,
        'Rafika'=>13,
        'Aicha'=>15,
        'Samir'=>13,
        'Samar'=>13,
        'Rafik'=>10,
        'Samiha'=>9,
        'Fourat'=>7,
        'Sami'=>7,
        'Noura'=>14);?>
        <table class='table table-striped'>
        <thead>
        <tr><th>Nom</th>
        <th>Note</th></tr>
        </thead>
        <tbody>
        <?php
        foreach ($notes_apprenants as $prenom =>$note)
        { 
        echo ("<tr><td>".$prenom."</td>"."<td>".$note."</td></tr>");
        }
        $notes_apprenants=['Naim'=>10,'Naima'=>12,'Nasib'=>10,];
        foreach ($notes_apprenants as $prenom =>$note)
        { 
        echo ("<tr><td>".$prenom."</td>"."<td>".$note."</td></tr>");
        }
        
        ?>
        </tbody>
        </table>
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
        
          </body>
        </html>

  4. Exercice 10
    1. Énoncé
    2. Solution
      • <!doctype html>
        <html lang="fr">
          <head>
            <!-- Required meta tags -->
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
        
        
            <title>Exercice10</title>
        <style>
        table, th, td {border: 1px solid black;
            border-collapse: separate;
            border-spacing: 4px;}
        thead> tr> th{background-color: #63B4DA;padding: 8px;}
        table{border-width: 4px;width:50%;}
        
        </style>
          </head>
          <body class="container">
        </br>
         <?php
        function estVoyelle($lettre)
        {
         $voyelle = array("A","E","I","O","U","Y");
          if (in_array($lettre, $voyelle))
         { 
          return "Voyelle";
         }
         else
         {
          return "Consonne";
         }
        }
        ?>
        <table>
              <thead >
              <tr><th>Numéro</th><th>Alphabet</th><th>Type</th><th>Code ASCII</th></tr>
        </thead><tbody>
          <?php
          $i = 1;
          for( $x = "A"; $i <= 26; $x++ ) {
            $type=estVoyelle($x);
            $style="";
            if($type=="Voyelle")
            {$style="style='background-color: #ccffff;'";}
            else{$style="style='background-color: #f5f5dc;'";}
            
            echo "<tr $style>";
            echo "<td>".$i."</td>";
            echo "<td>".$x."</td>";
            echo "<td>".ord($x)."</td>";
            echo "<td>".$type."</td>";
            echo "</tr>";
            $i++;
          }
        ?>
        <tbody>
        </table>
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
        
          </body>
          </html>

    3. Exercice 11
      1. Énoncé
      2. Solution
        • 
          <!doctype html>
          <html lang="fr">
              <head>
                  <!-- Required meta tags -->
                  <meta charset="utf-8">
                  <title>exercice 01</title>
              </head>
              <body>
          
          <?php
          // Déclaration d'un tableau vide
          $randTable = array();
          function remplirTable($count)
          {
              global $randTable;
              for($i = 0; $i <= $count; $i++) {
                  // Remplissage avec des valeurs entre 1 et 1000 (optionnel)
                  $randTable[] = mt_Rand(1,1000); 
              }
          }
          $somme=0;
          $sommePremier=0;
          $sommeParfait=0;
          //Vérifier si un nombre est premier
          function estNombrePremier($nombre) {
              //boucle de 2 au nombre à  tester
              for ($i = 2; $i < $nombre; $i++) {
                  //test du qutotien de la division
                  if ($nombre % $i == 0) {
                      return FALSE;
                  }
              }
              //Aucun diviseur trouvé, c'est un nombre permier
              return TRUE;
          }
          // Fonction pour vérifier si un nombre est parfait
          function estNombreParfait($N)
          {
              $sum = 0;
              for ($i = 1; $i < $N; $i++)
              {
                  if ($N % $i == 0)
                  {
                      $sum = $sum + $i;
                  }      
              }
              return $sum == $N;
          }
             
          
          remplirTable(9);
          echo "<table border='1'> <tr>";
          foreach($randTable as $nbr){
              echo "<td>$nbr</td>";
              $somme+=$nbr;
              if(estNombrePremier($nbr)) $sommePremier+=$nbr;
              if(estNombreParfait($nbr)) $sommeParfait+=1;
           }
           echo "</tr></table> ";
           echo "<br/>";
          echo "La somme des nombres du tableau: $somme</br>";
          echo "La somme des nombres premiers: $sommePremier</br>";
          echo "Le nombre des nombres parfaits: $sommeParfait</br>";
          //Triage du tableau
          rsort($randTable);
          echo "<br><table border='1'> <tr>";
          foreach($randTable as $nbr){
              echo "<td>$nbr</td>";
           }
           echo "</tr></table> ";
           echo "<br/>";
          ?>
          
          </body>
          </html>
          
          



Riadh HAJJI

Laisser un commentaire

Abonnez vous à notre chaîne YouTube gratuitement