Friday, August 28, 2015

Sum of natural number in PHP

/*
 * sum of natural number
 * */
$x = 8;
$result = $x;
while($x--){
  $result += $x;
}
echo $result;
/*
 * sum of natural number in another way
 * */
$x = 8;
$result = 0;
while($x){
  $result += $x--;
}
echo $result;

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }