programming-examples/php/Basics/PHP program to check a sequence of numbers is an arithmetic progression or not..php
2019-11-18 14:44:36 +01:00

20 lines
480 B
PHP

<?php
function is_arithmetic($arr)
{
$delta = $arr[1] - $arr[0];
for($index=0; $index<sizeof($arr)-1; $index++)
{
if (($arr[$index + 1] - $arr[$index]) != $delta)
{
return "Not an arithmetic sequence";
}
}
return "A arithmetic sequence";
}
$my_arr1 = array(6, 7, 9, 11);
$my_arr2 = array(5, 7, 9, 11);
print_r(is_arithmetic($my_arr1)."\n");
print_r(is_arithmetic($my_arr2)."\n");
?>