Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg1/exercises/ovning1/
index.php
153 lines UTF-8 Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Övning 1</title>
</head>
<body>
<h1>Gradomvandling Uppgift 1</h1>
<?php
$fahrenheit = 30;
$celsius = 5/9 * ($fahrenheit - 32);
echo("<p>".$fahrenheit." grader fahrenheit</p>");
echo("<p>".$celsius." grader celsius</p>");
?>
<h1>Uppgift 2</h1>
<?php
$text1 = "Kalle";
$enamn = "Anka";
$tal = 16;
$road = "Ankvägen";
echo("<p>".$text1." ".$enamn." bor på ".$road." ".$tal."</p>");
?>
<h1>Uppgift 3</h1>
<?php
$forSum = 0;
for ($i = 0; $i <= 100; $i++) {
$forSum += $i;
}
echo("For-loop: ".$forSum."<br>");
$whileSum = 0;
$times = 0;
while ($times <= 100) {
$whileSum += $times;
$times += 1;
}
echo("While-loop: ".$whileSum);
?>
<h1>Uppgift 4</h1>
<?php
$fahrenheit = array();
$celsius = array();
for ($i = -20; $i <= 40; $i++) {
if ($i % 10 !== 0) continue;
$f = (($i * 9/5) + 32);
echo($i."°C = ".$f."°F<br>");
$fahrenheit[] = $f;
$celsius[] = $i;
}
echo("<h2>Uppgift 4.1 * for-loop</h2>");
for ($i = 0; $i < count($celsius); $i++) {
echo("Celsius: ".$celsius[$i].". Fahrenheit: ".$fahrenheit[$i]."<br>");
}
echo("<h2>Uppgift 4.1 * var_dump</h2>");
echo(var_dump($celsius)."<br>");
echo(var_dump($fahrenheit));
echo("<h2>Uppgift 4.1 * print_r</h2>");
echo(print_r($celsius)."<br>");
echo(print_r($fahrenheit));
?>
<h1>Uppgift 5</h1>
<?php
$tal = array();
for ($i = 1; $i <= 10; $i++) {
if ($i == 1) {
$tal[] = $i;
continue;
}
$tal[] = $tal[$i - 2] * 2;
}
$sum = 0;
for ($i = 0; $i < 10; $i++) {
$sum += $tal[$i];
}
echo("Medelvärdet blir: ".$sum / 10);
?>
<h1>Uppgift 6</h1>
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i == 1) continue;
$primtal = true;
for ($del = 2; $del < $i; $del++) {
if ($i % $del == 0) {
$primtal = false;
break;
}
}
if ($primtal) {
echo($i." ");
}
}
echo("<br>");
?>
<h1>Uppgift 7</h1>
<?php
$primtal = array();
$tal = 2;
while (count($primtal) < 100) {
$p = true;
for ($del = 2; $del < $tal; $del++) {
if ($tal % $del == 0) {
$p = false;
break;
}
}
if ($p) $primtal[] = $tal;
$tal++;
}
for ($i = 0; $i <= count($primtal) - 1; $i++) {
echo($primtal[$i]." ");
}
?>
</body>
</html>