📂 LapTH — File Browser

🏠 Root / Buoi 3 / 4_2.php
File: 4_2.php — text/html

← Quay lại | ⬇️ Download | ▶️ Chạy file này (Tab mới)

Nội dung code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab 4_2</title>
</head>

<body>
<?php
	//Tính biểu thức S=1 + 2 + 3 + ... + 100
	// Sử dụng FOR
	$s1=0;
	for($i=1;$i<=100;$i++)
		$s1+=$i;
	//Sử dụng while
	$i=1;
	$s2=0;
	while($i<=100)
	{
		$s2+=$i;
		$i++;
	}
	//Sử dụng DO...WHILE
    $sum_n = 0;
    $n = 0;
	$i=1;
	$s3=0;
	do
	{
		$s3+=$i;
		$i++;
	}while($i<=100);
    do {
        $n++;
        $sum_n += $n;
    } while ($sum_n <= 1000);
	echo "Kết quả S = 1 + 2 + 3 + ... + 100 <br/>";
	echo "Tính bằng FOR, S1 = $s1 <br/>";
	echo "Tính bằng WHILE, S2 = $s2 <br/>";
	echo "Tính bằng DO...WHILE, S3 = $s3 <br/>";
    echo "<br>Giá trị n nhỏ nhất để 1 + 2 + ... + n > 1000 là: $n";
    echo "<br>Tổng S = $sum_n";
 ?>
</body>
</html>