Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

PHP program to print an arithmetic progression series using inbuilt functions

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

We have to print an arithmetic progressive series in PHP, between two given numbers a and b both including, a given common arithmetic difference of d


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 integers, or alphabets within a given range(from low to high) i.e, list’s first element is considered as low and the 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 a comma(,) separator. 


$a = 1;
$b = 100;
$d = 15;
$arr = range($a,$b,$d);
echo implode(", ",$arr);


1, 16, 31, 46, 61, 76, 91

Time complexity: O(n) // To print series up to nth tern

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Last Updated : 04 Aug, 2022
Like Article
Save Article
Similar Reads
Related Tutorials