By GokiSoft.com|
20:34 05/01/2024|
Học PHP
Tính tổng các số nguyên từ 0 tới N bằng PHP
Sinh ngẫu nhiên số tự nhiên N có giá trị từ 1 tới 100
Tính tổng các số nguyên từ 0->N và in kết quả ra màn hình
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-06-10 08:11:49
<?php
$n = random_int(0,100);
echo'radom: '.$n.'<br>';
$sum = 0;
for($i=0;$i<$n;$i++){
$sum += $i;
}
echo'sum: '.$sum;
?>
![lê văn phương [T1907A]](https://www.gravatar.com/avatar/a07ddfb51e1e7189c76b4e42dbdbcddc.jpg?s=80&d=mm&r=g)
lê văn phương
2020-06-10 05:17:38
<!DOCTYPE html>
<html>
<head>
<title>Tính tổng các số nguyên từ 0 -> N</title>
</head>
<body>
<?php
echo rand(1,100);
$sum =1;
mt_rand(1,100);
$n = rand(1,100);
for($x = 1;$x < $n;$x++){
$sum += $x;
}
echo "<br>";
echo "Số ngẫu nhiên : ".$n;
echo "<br>";
echo "Tổng các số từ 0 đến ".$n. " = " .$sum;
?>
</body>
</html>
![Đường Thanh Bình [T1907A]](https://www.gravatar.com/avatar/c2ef7c316acb82467912bf5677b52a8b.jpg?s=80&d=mm&r=g)
Đường Thanh Bình
2020-06-08 14:12:04
<!DOCTYPE html>
<html>
<head>
<title>Tính tổng các số nguyên</title>
</head>
<body>
<?php
$sum = 0;
mt_rand(1,100);
$n = rand(1,100);
for($r =1;$r < $n+1; $r++)
{
$sum += $r;
}
echo "Random Number: ".$n;
echo "<br>";
echo "Tổng các số từ 1 -> ".$n.".$sum";
?>
</body>
</html>
![Minh Nghia [T1907A]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
Minh Nghia
2020-06-08 13:49:04
<!DOCTYPE html>
<html>
<head>
<title>Sum of whole numbers</title>
</head>
<body>
<?php
$number = mt_rand(1,100);
$total = 0;
for($x = 1; $x <= $number; $x++){
$total += $x;
}
echo "Random Number $number"."<br>";
echo "Random total 1 ==> $number : $total" ;
?>
</body>
</html>
![nguyễn văn huy [T1907A]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
nguyễn văn huy
2020-06-08 10:36:36
<html>
<head>
<title>Tính tổng dãy số trong PHP</title>
</head>
<body>
<?php
$sum = 0;
for($x=1; $x <= 20; $x++)
{
$sum +=$x;
}
echo "Tổng dãy số từ 1 đến 20 là $sum"."<br>";
?>
</body>
</html>
![hoangduyminh [T1907A]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
hoangduyminh
2020-06-08 09:59:21
<!DOCTYPE html>
<html>
<head>
<title>Tinh Tong So Nguyen</title>
</head>
<body>
<?php
$sum = 0;
$i = rand(0,100);
for ($x = 1; $x < $i; $x++){
$sum += $x;
}
echo "So Ngau Nhien".$i;
echo "<br>";
echo "Tong cac so tu 0 den x la".$sum;
?>
</body>
</html>
![Phan Bạch Tùng Dương [T1907A]](https://www.gravatar.com/avatar/e74e3ec62fe3a191929e12eecbe01edf.jpg?s=80&d=mm&r=g)
Phan Bạch Tùng Dương
2020-06-08 09:55:34
<!DOCTYPE html>
<html>
<head>
<title>NumRandom</title>
</head>
<body>
<?php
$sum = 0;
mt_rand(1,100);
$n = rand(1,100);
for($r = 1;$r < $n+1; $r++){
$sum += $r;
}
echo "Random number: ".$n;
echo "<br>";
echo "Tong cac so tu 1 -> ".$n."=".$sum;
?>
</body>
</html>
![Phạm Ngọc Minh [T1907A]](https://www.gravatar.com/avatar/9fa44938f36838ed4b4ac8e30e070c74.jpg?s=80&d=mm&r=g)
Phạm Ngọc Minh
2020-06-08 09:49:52
<!DOCTYPE html>
<html>
<head>
<title>Tổng các số nguyên</title>
</head>
<body>
<?php
function Sum($a){
if ($a==1) {
return 1;
}else {
return $a + Sum($a-1);
}
}
$a = rand(1, 100);
print "random: ".$a;
print "<br>";
print "Tong 1 den ".$a." la : ".Sum($a);
?>
</body>
</html>
![Trần Anh Quân [T1907A]](https://www.gravatar.com/avatar/7e3a8fe30cedd711f426fc59a9d48efc.jpg?s=80&d=mm&r=g)
Trần Anh Quân
2020-06-08 09:41:23
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>Tinh tong 0->N</title>
</head>
<body>
<?php
function Sum($n){
if ($n==1) {
return 1;
}else {
return $n + Sum($n-1);
}
}
$n = rand(1, 100);
echo "So ngau nhien: ".$n;
echo "<br>";
echo "Tong cac so tu 1 den ".$n." la: ".Sum($n);
?>
</body>
</html>
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-06-08 09:38:15
<!DOCTYPE html>
<html>
<head>
<title>Tinh tong cac so nguyen tu 0 toi N bang PHP</title>
</head>
<body>
<?php
echo rand(1,100);
$sum =0;
mt_rand(1,100);
$n = rand(1,100);
for($i = 1;$i < $n;$i++){
$sum += $i;
}
echo "So ngau nhien : ".$n;
echo "Tong cac so tu 1 den".$n."=".$sum;
?>
</body>
</html>