Differences
This shows you the differences between two versions of the page.
|
dmprog_07_ex1 [2017-09-01 12:54] |
dmprog_07_ex1 [2022-07-18 11:20] (current) |
||
|---|---|---|---|
| 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(' | ||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | |||