Friday, August 28, 2015

sum of square of natural number

/*
 * sum of square of natural number
 * */
$n = 5;
$sum = 0;
$i = 1;
while($n){
   $i = $n;
   $sum += $i ** 2;
   $n--;
}
echo $sum;


/*
 * sum of square of natural number in another way
 * */
$n = 5;
$sum = 0;
$i = 1;
while($n){
   $sum += ($n * $n);
   $n--;
}

echo $sum;

No comments:

Post a Comment

css snippet for blogger code highlighting

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