Multiplication table for loop continuous summation, multiplication table code

WBOY
Release: 2016-07-29 08:48:05
Original
1733 people have browsed it

The classic example of a for loop is continuous summation: 1+2+3+...+100. After teaching for more than an hour, some students still can't do it. You have to be thoughtful when making programs. Some students kept typing on the keyboard and couldn't get it right. Before doing this summation, we have to think about it. Summation is actually continuous accumulation. When variable $i increases by itself, it must be summed with the previous number. So how to sum with the previous number? We can do a split: treat the number before $i as one item, and add it to $i separately. In the same way, 100 is added to the sum of the previous 99 items, 99 is added to the sum of the previous 98 items...and so on. , 2 plus the previous number 1, then 1 is 1+0. When writing a program, you need to think in reverse. First calculate 0+1=1, then 1+2=3, then 3+3=6...

Copy the code The code is as follows:


< ;?php
/*
*file name: 1+...+100.php
*author: luchanghong
*email: luchanghong@xingmo.com
*time: 2011/5/24
*/
$sum = 0;
$str = '';
for($i = 0 ; $i <= 100 ; ++$i)
{
echo $str .= $i.'+';
// echo '< ;br>';
// echo $sum.'+'.$i.'=';
echo '=';
echo $sum = $sum+$i;
echo '
';
}
echo $sum;
?>


The echo statement in the middle of the loop body is for testing the process, so you can see it more clearly.
The multiplication table below uses a two-layer for loop. It may be more difficult for novices, but it can still be understood if you study patiently and think attentively.

Copy code The code is as follows:


/*
*file name: 99.php
*author: luchanghong
*email: luchanghong@xingmo.com
*time: 2011/5 /9
*/
echo '

';
for($i = 1 ; $i<10 ; ++$i)
{
echo '';
for($ j = 1 ; $j<= $i ; ++$j)
{
echo '';
}
echo '
'.$j.'x'.$i.'='.$j*$i.'< /td>';
}
echo '
';
?>

The above has introduced the multiplication table for loop continuous summation and the multiplication table code, including the content of the multiplication table. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!