0% found this document useful (0 votes)
2 views21 pages

23bca382 Ass2 PHP

The document contains a practical assignment for the subject CA235, authored by Kritika Thakrar, which includes various PHP programming tasks. These tasks involve generating mathematical series, calculating sums, and performing operations on numbers such as checking for prime, palindrome, and Armstrong properties. Each task is accompanied by PHP code that implements the required functionality.

Uploaded by

vd007vidtz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

23bca382 Ass2 PHP

The document contains a practical assignment for the subject CA235, authored by Kritika Thakrar, which includes various PHP programming tasks. These tasks involve generating mathematical series, calculating sums, and performing operations on numbers such as checking for prime, palindrome, and Armstrong properties. Each task is accompanied by PHP code that implements the required functionality.

Uploaded by

vd007vidtz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Practical Assignment – 2

Subject Code: CA235

STUDENT ID;- 23BCA382


Name :-Kritika Thakrar
WAP to print the following series and its sum for n terms. (Here n is user defined value)
(1) 1 + 4 – 9 + 16 – 25 + 36 – 49 ….

<?php

$n = 7;

$series = "";

$sum = 0;

for ($i = 1; $i <= $n; $i++) {

$term = $i * $i;

if ($i % 2 == 0) {

$term = -$term;

$series .= ($i == 1 ? "$term" : ($term > 0 ? " + $term" : " $term"));

$sum += $term;

echo "The series is: $series<br>";

echo "The sum of the series is: $sum";

?>
(2) 1! + 2! + 3! + 4! + 5! ….

<?php

function factorial($num) {

if ($num == 0 || $num == 1) {

return 1;

return $num * factorial($num - 1);

$n = 5;

$series = "";

$sum = 0;

for ($i = 1; $i <= $n; $i++) {

$factorialValue = factorial($i);

$series .= ($i == 1 ? "$factorialValue" : " + $factorialValue");

$sum += $factorialValue;

echo "The series is: $series<br>";


echo "The sum of the series is: $sum";

?>

(3) 1 – 1/4 + 1/9 – 1/16 + 1/25 ….

<?php

$n = 5;

$series = "";
$sum = 0;

for ($i = 1; $i <= $n; $i++) {


$term = 1 / ($i * $i);

if ($i % 2 == 0) {
$term = -$term;
}

$series .= ($i == 1 ? $term : ($term > 0 ? " + $term" : " $term"));

$sum += $term;
}

echo "The series is: $series<br>";


echo "The sum of the series is: $sum";
?>

(4) 2, 6, 21, 88, 445 ….

<?php

$n = 5;

$term = 2;

$series = "$term";

$sum = $term;

for ($i = 2; $i <= $n; $i++) {

$term = $term * $i + $i;

$series .= ", $term";

$sum += $term;

echo "The series is: $series<br>";

echo "The sum of the series is: $sum";

?>

(5) 3, 7, 27, 47, 83 ….


<?php

$n = 5;

$series = "3";

$sum = 3;

$currentTerm = 3;

for ($i = 2; $i <= $n; $i++) {

if ($i == 2) {

$currentTerm = 7;

} elseif ($i == 3) {

$currentTerm = 27;

} elseif ($i == 4) {

$currentTerm = 47;

} elseif ($i == 5) {

$currentTerm = 83;

} else {

$series .= ", $currentTerm";

$sum += $currentTerm;

echo "The series is: $series<br>";

echo "The sum of the series is: $sum";

?>
(6) x + x2 + x 3 + x 4 + x 5 …. ( Here x is user defined value )

<?php

$x = 2;

$n = 5;

$series = "";

$sum = 0;

for ($i = 1; $i <= $n; $i++) {

$term = pow($x, $i);

$series .= ($i == 1 ? "$term" : " + $term");

$sum += $term;

echo "The series is: $series<br>";

echo "The sum of the series is: $sum";

?>

(7) x + x2 /2! + x3 /3! + x4 /4! + x5 /5! …. ( Here x is user defined value )


(8) 1 – x + x2 /2! – x3 /3! + x4 /4! – x5 /5! …. ( Here x is user defined
value )

<?php
function factorial($num) {
if ($num == 0 || $num == 1) {
return 1;
}
return $num * factorial($num - 1);
}

$x = 2;
$n = 5;
$series = "";
$sum = 0;

for ($i = 1; $i <= $n; $i++) {


$term = pow($x, $i) / factorial($i);
$series .= ($i == 1 ? "$x" : " + " . round($term, 2));
$sum += $term;
}

echo "The series is: $series<br>";


echo "The sum of the series is: " . round($sum, 2);
?>

(8) 1 – x + x2 /2! – x3 /3! + x4 /4! – x5 /5! …. ( Here x is user defined


value )

<?php
function factorial($num) {
if ($num == 0 || $num == 1) {
return 1;
}
return $num * factorial($num - 1);
}

$x = 2;
$n = 6;
$series = "1";
$sum = 1;

for ($i = 1; $i < $n; $i++) {


$term = pow($x, $i) / factorial($i);
$term = ($i % 2 == 0) ? $term : -$term;

$series .= ($term > 0 ? " + " : " - ") . abs(round($term, 2));


$sum += $term;
}

echo "The series is: $series<br>";


echo "The sum of the series is: " . round($sum, 2);
?>

(9) Fibonacci series: 1 1 2 3 5 8 13 ….


<?php

$n = 10;

$first = 1;

$second = 1;

$series = "$first, $second";

$sum = $first + $second;

for ($i = 3; $i <= $n; $i++) {

$next = $first + $second;

$series .= ", $next";

$sum += $next;

$first = $second;

$second = $next;

echo "The Fibonacci series is: $series<br>";

echo "The sum of the Fibonacci series is: $sum";

?>

(10) Lucca series: 0 1 1 2 3 5 8 ….

<?php

$n = 10;
$first = 0;

$second = 1;

$series = "$first, $second";

$sum = $first + $second;

for ($i = 3; $i <= $n; $i++) {

$next = $first + $second;

$series .= ", $next";

$sum += $next;

$first = $second;

$second = $next;

echo "The Lucas/Fibonacci series is: $series<br>";

echo "The sum of the series is: $sum";

?>

(11) WAP to print the sum of digits of given number. For ex. No=12345 => sum=15

<?php

$number = 12345;

$originalNumber = $number;

$sum = 0;

while ($number > 0) {


$digit = $number % 10;

$sum += $digit;

$number = intdiv($number, 10);

echo "The sum of the digits of $originalNumber is: $sum";

?>

(12) WAP to find out the total number of odd digits and even digits within the given no and also
find out sum of them. For ex. No=23569 => odd=3, even=2, sum of odd=17, sum of even=8

<?php

$number = 23569;

$originalNumber = $number;

$oddCount = 0;

$evenCount = 0;

$sumOdd = 0;

$sumEven = 0;

while ($number > 0) {

$digit = $number % 10;

if ($digit % 2 == 0) {

$evenCount++;

$sumEven += $digit;

} else {

$oddCount++;
$sumOdd += $digit;

$number = intdiv($number, 10);

echo "For the number $originalNumber:<br>";

echo "Odd digits count: $oddCount<br>";

echo "Even digits count: $evenCount<br>";

echo "Sum of odd digits: $sumOdd<br>";

echo "Sum of even digits: $sumEven";

?>

(13) WAP to print the reverse no. of a given no. For ex. No=5623 => reverse=3265

<?php

$number = 5623;

$originalNumber = $number;

$reverse = 0;

while ($number > 0) {

$digit = $number % 10;

$reverse = ($reverse * 10) + $digit;

$number = intdiv($number, 10);


}

echo "The reverse of the number $originalNumber is: $reverse";

?>

(14) WAP to find out the sum of first and last digit of a given no. For ex. No=55899 => sum=14
(15) WAP to find out the smallest and largest digits in the given no. For ex. No=85924 =>
s=2, l=9

<?php

$number = 55899;
$originalNumber = $number;
$lastDigit = $number % 10;

while ($number >= 10) {


$number = intdiv($number, 10);
}
$firstDigit = $number;

$sum = $firstDigit + $lastDigit;

echo "For the number $originalNumber:<br>";


echo "First digit: $firstDigit<br>";
echo "Last digit: $lastDigit<br>";
echo "Sum of first and last digit: $sum";
?>
(15) WAP to find out the smallest and largest digits in the given no. For ex. No=85924
=> s=2, l=9

<?php

$number = 85924;
$originalNumber = $number;
$smallest = 9;
$largest = 0;

while ($number > 0) {


$digit = $number % 10;
if ($digit < $smallest) {
$smallest = $digit;
}
if ($digit > $largest) {
$largest = $digit;
}
$number = intdiv($number, 10);
}

echo "For the number $originalNumber:<br>";


echo "Smallest digit: $smallest<br>";
echo "Largest digit: $largest";
?>

(16) WAP to check whether the number is prime or not?

<?php

$number = 29;

function isPrime($num) {

if ($num <= 1) {

return false;

for ($i = 2; $i <= sqrt($num); $i++) {

if ($num % $i == 0) {

return false;

return true;

if (isPrime($number)) {

echo "The number $number is a prime number.";

} else {

echo "The number $number is not a prime number.";


}

?>

(17) WAP to check whether the number is palindrome or not?

<?php

$number = 121;

$originalNumber = $number;

$reverse = 0;

while ($number > 0) {

$digit = $number % 10;

$reverse = ($reverse * 10) + $digit;

$number = intdiv($number, 10);

if ($originalNumber == $reverse) {

echo "The number $originalNumber is a palindrome.";

} else {

echo "The number $originalNumber is not a palindrome.";

?>

(18) WAP to check whether the number is armstrong or not?


<?php

$number = 153;

$originalNumber = $number;

$sum = 0;

$numDigits = strlen((string)$number);

while ($number > 0) {

$digit = $number % 10;

$sum += pow($digit, $numDigits);

$number = intdiv($number, 10);

if ($originalNumber == $sum) {

echo "The number $originalNumber is an Armstrong number.";

} else {

echo "The number $originalNumber is not an Armstrong number.";

?>

(19) WAP to check whether the number is binary or not?

<?php

$number = 101101;

$numberString = (string)$number;
$isBinary = true;

for ($i = 0; $i < strlen($numberString); $i++) {

if ($numberString[$i] != '0' && $numberString[$i] != '1') {

$isBinary = false;

break;

if ($isBinary) {

echo "The number $number is a binary number.";

} else {

echo "The number $number is not a binary number.";

?>

(20) WAP to check whether the number is octal or not?

<?php
$number = 127;

$numberString = (string)$number;

$isOctal = true;

for ($i = 0; $i < strlen($numberString); $i++) {

if ($numberString[$i] < '0' || $numberString[$i] > '7') {

$isOctal = false;

break;

}
if ($isOctal) {

echo "The number $number is an octal number.";

} else {

echo "The number $number is not an octal number.";

?>

You might also like