PHP Series Program

Below are some series based practice program that can be practiced for interviews:

Series:- 1 + 2 + 3 + 4 + 5 + … + n

// 1 + 2 + 3 + 4 + 5 + ... + n

$n = 10;
for ($i = 1; $i <= $n; $i++) {
 $sum+=$i;
}
echo "Sum of 1 + 2 + 3 + 4 + 5 + ... + n is " . $sum;

 

Series:- (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)


// (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
$n = 10;
for ($i = 1; $i <= $n; $i++) {
 $sum+=$i * $i;
}
echo "Sum of (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n) is " . $sum;

 

Series:- (1) + (1+2) + (1+2+3) + (1+2+3+4) + … + (1+2+3+4+…+n)

// (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)
$n = 10;
for ($i = 1; $i <= $n; $i++) {
 for ($j = 1; $j <= $i; $j++) {
 $sum+=$j;
 }
}
echo "Sum of (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) is " . $sum;

 

Series:- 1! + 2! + 3! + 4! + 5! + … + n!

//1! + 2! + 3! + 4! + 5! + ... + n!
function fact($n) {
 $f = 1;
 for ($i = 1; $i <= $n; $i++) {
 $f = $f * $i;
 }

 return $f;
}

$sum = 0;
$n = 5;
for ($i = 1; $i <= $n; $i++) {
 $sum = $sum + fact($i);
}
echo "Sum of 1! + 2! + 3! + 4! + 5! + ... + n! is " . $sum;

 

Series:- (1^1) + (2^2) + (3^3) + (4^4) + (5^5) + … + (n^n)

// (1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)
function power($a, $b) {
 $p = 1;
 for ($i = 1; $i <= $b; $i++) {
 $p = $p * $a;
 }
 return $p;
}

$sum = 0;
$n = 5;
for ($i = 1; $i <= $n; $i++) {
 $sum = $sum + power($i, $i);
}
echo "Sum of (1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n) is " . $sum;

 

Series:- (1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + … + (n!/n)

// (1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)
function factorial($n) {
 $f = 1;
 for ($i = 1; $i <= $n; $i++) {
 $f = $f * $i;
 }
 return $f;
}

$sum = 0;
$n = 5;
for ($i = 1; $i <= $n; $i++) {
 $sum = $sum + (fact($i) / $i);
}
echo "Sum of (1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n) is " . $sum;

 

Series:-  [(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + … + [(n^n)/n]

// [(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n]

function pow($a, $b) {
 $p = 1;
 for ($i = 1; $i <= $b; $i++) {
 $p = $p * $a;
 }
 return $p;
}

$sum = 0;
$n = 5;
for ($i = 1; $i <= $n; $i++) {
 $sum = $sum + (pow($i, $i) / $i);
}
echo "Sum of [(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n] is " . $sum;

 

Series:- [(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + … + [(n^n)/n!]

// [(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + ... + [(n^n)/n!]
function power($a, $b) {
 $p = 1;
 for ($i = 1; $i <= $b; $i++) {
 $p = $p * $a;
 }
 return $p;
}

function fact($n) {
 $f = 1;
 for ($i = 1; $i <= $n; $i++) {
 $f = $f * $i;
 }
 return $f;
}

$sum = 0;
$n = 5;
for ($i = 1; $i <= $n; $i++) {
 $sum = $sum + power($i, $i) / fact($i);
}
echo "Sum of [(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + ... + [(n^n)/n!] is " . $sum;

 

Series:- 1/2 – 2/3 + 3/4 – 4/5 + 5/6 – …… n


// 1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n
$sum = 0;
$n = 10;
for ($i = 1; $i <= $n; $i++) {
 if ($i % 2 == 1)
 $sum+=$i / ($i + 1);
 else
 $sum-=$i / ($i + 1);
}
echo "Sum of 1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n is " . $sum;

Series:- 1 2 3 6 9 18 27 54…

// 1 2 3 6 9 18 27 54...
$a = 1;
$b = 2;
$n = 10;

for ($i = 3; $i <= $n; $i++) {
 if ($i % 2 == 1) {
 $a = $a * 3;
 echo $a . ' ';
 } else {
 $b = $b * 3;
 echo $b . ' ';
 }
}

Series:- 2 15 41 80 132 197 275 366 470 587

// 2 15 41 80 132 197 275 366 470 587

$a = 2;
$n = 10;
for ($i = 1; $i <= $n; $i++) {
 echo $a . ' ';
 $a+=13 * $i;
}

Leave a Comment.