+ 2
How to print sum in decreasing way?
I want to print sum of two numbers like this: 15,14,12,9,5
4 odpowiedzi
+ 8
The de facto way to solve this problem is using a loop structure with good observation on the number pattern.
Can you show us what you've tried so we can guide you from there? 😉
+ 5
You can use an array as temporary buffer and outputs its contents in reverse later, as follows:
<?php
$a = 5;
$b = 1;
$sum = 0;
// Here we create an empty array to contain
// the $sum values temporarily
$temp = array();
for($a; $a >= $b; $a--)
// Add the $sum into the array
$temp[] = ($sum += $a);
// Output the array contents in reverse
$limit = count($temp);
while($limit--)
echo "$temp[$limit]<br />";
?>
Hth, cmiiw
+ 3
I solved!