Differences
This shows you the differences between two versions of the page.
amprog_07_ex1 [2019-09-09 09:23] Daniel Viström |
amprog_07_ex1 [2022-07-18 13:20] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Exempel - Tal på utvecklad form ====== | ||
- | |||
- | |||
- | <code php> | ||
- | <?php | ||
- | include ' | ||
- | |||
- | // Dela upp ett 4-siffrigt tal i separata siffor (skriv ut i utvecklad form) | ||
- | $tal = mt_rand(1000, | ||
- | |||
- | 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 ' | ||
- | ?> | ||
- | </ | ||
- | |||
- | |||