Files
2p_pracownia_programowania_php/Zadania/Z88 - Średnia N liczb/index.php
2025-10-31 22:15:27 +01:00

52 lines
1.6 KiB
PHP

<?php
?>
<!doctype html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Z88 - Średnia N liczb</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Zadanie Z88</h1>
<h2>Autor: Jakub Grzegorczyk</h2>
</header>
<div class="box">
<p>Napisz program, który pyta o liczbę całkowitą N, a następnie oblicza sumę i średnią N liczb i wyświetla wynik na ekranie.</p>
</div>
<div class="box">
<form action="index.php" method="post">
<fieldset>
<label for="n">Podaj N:</label>
<input type="text" name="n" id="n" required min="1">
<label for="spaced_numbers">Podaj N liczb oddzielonych spacją:</label>
<input type="text" name="spaced_numbers" id="spaced_numbers" required>
</fieldset>
<button type="submit">Wyślij</button>
</form>
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$n = htmlspecialchars($_POST['n']);
$spaced_numbers = htmlspecialchars($_POST['spaced_numbers']);
echo '<div class="box">';
$arr = explode(' ', $spaced_numbers);
$suma = 0;
$liczba = 0;
echo 'Suma liczb w tablicy: <br>';
for ($i = 0; $i < $n; $i++) {
$suma += (int)$arr[$i];
echo $arr[$i];
if ($i < $n - 1) { echo ' + '; }
}
echo " = <b>$suma</b><br>";
echo "Średnia: <b>" . $suma / $n . "</b><br>";
}
?>
</body>
</html>