Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dmprog_07_ex1 [2017-09-01 14:54]
dmprog_07_ex1 [2022-07-18 13:20] (current)
Line 1: Line 1:
 +====== Exempel - Tal på utvecklad form ======
 +
 +
 +<code php>
 +<?php
 +include('head.php');                  
 +
 +// Dela upp ett 4-siffrigt tal i separata siffor (skriv ut i utvecklad form)
 +$tal = mt_rand(1000,9999);
 +
 +echo 'Talet är: ' . $tal . ' = ';
 +
 +$i = 1000;                        // Börja med tusental
 +while ($tal > 0){
 +  $siffra = (int)($tal / $i);     // Ger heltal i division
 +  $tal = $tal % $i;               // Resten ska divideras nästa varv.
 +  
 +  if ($siffra != 0 ){
 +    echo $siffra . ' * ' . $i;    // Skriv aktuell term
 + 
 +    if ($tal != 0){               // Om det inte är sista varvet
 +      echo ' + ';
 +    }  
 +  }
 +  $i = $i / 10;                   
 +}
 +
 +include('foot.php');
 +?>
 +</code>
 +
 +