How to print sum in decreasing way? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to print sum in decreasing way?

I want to print sum of two numbers like this: 15,14,12,9,5

25th Feb 2018, 3:41 PM
Dejan Francuz🥇
Dejan Francuz🥇 - avatar
4 Answers
+ 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? 😉
25th Feb 2018, 4:04 PM
Zephyr Koo
Zephyr Koo - avatar
+ 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
25th Feb 2018, 6:44 PM
Ipang
+ 3
I solved!
25th Feb 2018, 10:27 PM
Dejan Francuz🥇
Dejan Francuz🥇 - avatar
25th Feb 2018, 5:22 PM
Dejan Francuz🥇
Dejan Francuz🥇 - avatar