We have to print an arithmetic progressive series in PHP, between two given numbers a and b both including, with a given common arithmetic difference of d.
Examples:
Input : $a = 200, $b = 250, $d = 10 Output : 200, 210, 220, 230, 240, 250 Input : $a = 10, $b = 100, $d = 20 Output : 10, 30, 50, 70, 90
This problem can be solved using loops by iterating from $a to $b and incrementing the loop variable by $d. But in PHP we can also make use of some inbuilt functions to solve this particular problem.
We will have to use the below two functions for this purpose:
- range() function: This function is used to create an array of elements of any kind such as integer, alphabets within a given range(from low to high) i.e, listâs first element is considered as low and last one is considered as high.
- implode() function: If we have an array of elements, we can use the implode() function to join them all to form one string. We basically join array elements with a string.
The idea to solve this problem using the above two inbuilt functions is to first use the range() function to generate an array of values between $a and $b where the values are incremented by $d. After generating the array we will use the implode() function to create a string from the array where elements will be separated by comma(,) separator.
<?php $a = 1; $b = 100; $d = 15; $arr = range($a,$b,$d); echo implode(", ",$arr); ?> |
Output:
1, 16, 31, 46, 61, 76, 91
Recommended Posts:
- Program to print Arithmetic Progression series
- Program for N-th term of Arithmetic Progression series
- Longest Arithmetic Progression | DP-35
- Check whether Arithmetic Progression can be formed from the given array
- Count of AP (Arithmetic Progression) Subsequences in an array
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Longest arithmetic progression with the given common difference
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Convert given array to Arithmetic Progression by adding an element
- Arithmetic Progression
- Change one element in the given array to make it an Arithmetic Progression
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Count common elements in two arrays which are in Arithmetic Progression
- Find the missing number in unordered Arithmetic Progression
- Count of subarrays forming an Arithmetic Progression (AP)
- Longest subarray forming an Arithmetic Progression (AP)
- Arithmetic Progression containing X and Y with least possible first term
- Minimize Nth term of an Arithmetic progression (AP)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

