0% found this document useful (0 votes)
11 views

php tushar singh

The document contains a series of PHP programming exercises related to web technologies, including tasks such as finding the sum of digits, removing duplicates from an array, sorting arrays, and checking for Armstrong numbers. Each exercise includes a PHP code snippet along with its expected output. The document is structured with multiple questions, each followed by a program and its output.

Uploaded by

ponima5849
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)
11 views

php tushar singh

The document contains a series of PHP programming exercises related to web technologies, including tasks such as finding the sum of digits, removing duplicates from an array, sorting arrays, and checking for Armstrong numbers. Each exercise includes a PHP code snippet along with its expected output. The document is structured with multiple questions, each followed by a program and its output.

Uploaded by

ponima5849
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/ 27

NAME: Tushar Singh PRN No: ADT24MGTM0993

DIV: C CLASS: MCA-I Sem-II (DS)


SUBJECT: Web Technologies DATE: 19/03/2025

Q1Write a PHP program to find the sum of digits of a given number.

<?php
function sumOfDigits($number) {
$sum = 0;
$number = abs($number); while ($number > 0) {
$digit = $number % 10;
$sum += $digit;
$number = (int)($number / 10);
} return

$sum;

$number = 12345;
echo "The sum of digits of $number is: " . sumOfDigits($number); ?
>

Output:-
NAME:Tushar Singh PRN No: ADT24MGTM0993
DIV: C CLASS: MCA-I Sem-II
(DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q2. Write a PHP script to remove duplicate elements from an array.

Program:-
<?php

$originalArray = array(1, 2, 2, 3, 4, 4, 5, 1, 6);


$uniqueArray = array_unique($originalArray);
$uniqueArray =
array_values($uniqueArray); echo "Original
Array: "; print_r($originalArray); echo
"Array after removing duplicates: ";
print_r($uniqueArray);
?>

Output:-
NAME:Tushar Singh PRN No: ADT24MGTM0993
DIV: C CLASS: MCA-I Sem-II
(DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Write a PHP program to sort an array in ascending order without using built-
in functions.

Program:-
<?php
function bubbleSortAscending(&$array) {
$n = count($array); for ($i = 0; $i
< $n - 1; $i++) { for ($j = 0; $j <
$n - $i - 1; $j++)
{ if ($array[$j] > $array[$j +
1]) {
// Swap the elements
$temp = $array[$j];
$array[$j] = $array[$j + 1];
$array[$j + 1] = $temp;
}

$numbers = array(5, 2, 9, 1, 5, 6);


echo "Original Array: ";
print_r($numbers);
bubbleSortAscending($numbers);
echo "Sorted Array (Ascending):
"; print_r($numbers);
?>
NAME:Tushar Singh PRN No: ADT24MGTM0993
DIV: C CLASS: MCA-I Sem-II
(DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Output:-

Q4. Write a PHP script to merge two arrays and sort them.

Program:-
<?php
$array1 = array(3, 1, 7);
$array2 = array(4, 2, 6);
$mergedArray = array_merge($array1, $array2);
sort($mergedArray); // This is a built-in function for
sorting print_r($array1); echo "Array 2: ";
print_r($array2); echo "Merged and Sorted Array: ";
print_r($mergedArray);
?>

Output:-
NAME:Tushar Singh PRN No: ADT24MGTM0993
DIV: C CLASS: MCA-I Sem-II
(DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Write a PHP program to check if an array contains a specific value.

Program:-
<?php

$fruits = array("apple", "banana", "orange", "grape");


$searchValue = "orange";

if (in_array($searchValue, $fruits)) {
echo "The value '$searchValue' exists in the array.";
} else { echo "The value '$searchValue' does NOT exist in the
array.";
}

?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q6. Write a PHP script to find the second largest number in an


array.
Program:-
<?php function
findSecondLargest($array) {
$largest = $secondLargest = null; foreach
($array as $num) { if ($largest === null ||
$num > $largest) {
$secondLargest = $largest;
$largest = $num;
} elseif ($num != $largest && ($secondLargest === null || $num > $secondLargest))
{ $secondLargest = $num;
} }

return $secondLargest;
}

$numbers = array(4, 7, 2, 9, 5, 9, 1);


$secondLargest = findSecondLargest($numbers);
if ($secondLargest !== null) {
echo "The second largest number is: $secondLargest";
} else { echo "Second largest number not found (array may have all identical values or fewer
than 2
elements).";
}
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q7. Write

?>

Output:-

a PHP program to check if a number is an Armstrong number.

Program:-
<?php
function isArmstrong($number) {
$numStr = strval($number);
$numDigits = strlen($numStr);
$sum = 0;
$temp = $number;
while ($temp > 0) {
$digit = $temp % 10;
$sum += pow($digit, $numDigits);
$temp = (int)($temp / 10);
}
return $sum === $number;
}

$number = 153; if
(isArmstrong($number)) {
echo "$number is an Armstrong number.";
} else { echo "$number is NOT an Armstrong
number.";
}
?>
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q8. Write

Output:-

script to convert a string to uppercase without using built-in functions.

Program:-
<?php function
toUppercase($str) {
$uppercaseStr = "";
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
$ascii = ord($char);
if ($ascii >= 97 && $ascii <= 122) {

$uppercaseStr .= chr($ascii - 32);


} else {
$uppercaseStr .= $char;
}

return $uppercaseStr;
}
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q9. Write

$input = "Hello World!"; echo


"Original String: $input\n";
echo "Uppercase String: " . toUppercase($input);
?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q10. Write a PHP script to

check if a given number is even or odd.

Program:-
<?php
function checkEvenOrOdd($number)
{ if ($number % 2 == 0)
{ echo "$number is Even.";
} else { echo "$number is
Odd.";
}

$number = 7;
checkEvenOrOdd($number);
?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q11. Write a PHP script to


Program:-
<?php

program to find the largest of three numbers.

Progra
m:- <?php

function findLargest($a, $b, $c) {


if ($a >= $b && $a >= $c) {
return $a;
} elseif ($b >= $a && $b >= $c)
{ return $b;
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q12. Write a PHP script to


Program:-
<?php

} else
{ return $c;
}

$num1 = 45;
$num2 = 78;
$num3 = 23;
$largest = findLargest($num1, $num2, $num3); echo "The
largest of $num1, $num2, and $num3 is: $largest";
?>

Output:-
check if a given string is a palindrome.
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q13. Write a PHP script to


Program:-
<?php

function isPalindrome($string) {
$cleaned = strtolower(str_replace(' ', '', $string));
$reversed = '';
for ($i = strlen($cleaned) - 1; $i >= 0; $i--)
{ $reversed .= $cleaned[$i]; }
$cleaned === $reversed;}

$input = "Madam"; if
(isPalindrome($input)) {
echo "'$input' is a palindrome.";
} else { echo "'$input' is NOT a
palindrome.";
}
?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q14. Write a PHP script to


Program:-
<?php
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q15. Write a PHP


Program:-

program to count the number of words in a string.

<?php function
countWords($string) {
$trimmed = trim($string);

($trimmed === '')


{ return 0;
}

$words = explode(' ', $trimmed);


$words = array_filter($words, function($word)
{ return $word !== '';});
return count($words);}
$input = " Hello world, this is a PHP script. ";
echo "Input String: \"$input\"\n"; echo "Word
Count: " . countWords($input);
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q16. Write a PHP script to


Program:-
<?php

?>

Output:-
reverse a string without using built-in functions.

function reverseString($string) {
$reversed = '';
$length = strlen($string); for ($i =
$length - 1; $i >= 0; $i--)
{ $reversed .= $string[$i];
}

return $reversed;
}
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q17. Write a PHP script to


Program:-
<?php

$input = "Hello, PHP!"; echo "Original String:


$input\n"; echo "Reversed String: " .
reverseString($input);
?>

Output:-

calculate the factorial of a number using recursion.

<?php function
factorial($n) { // Base
case if ($n <= 1)
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q18. Write a PHP script to


Program:-
<?php

{ return 1;
} return $n * factorial($n - 1);
}

$number = 5; echo "The factorial of $number is: " .


factorial($number);
?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q19. Write a PHP


Program:-
<?php

program to check whether a number is prime or not.

function isPrime($number) {
if ($number <= 1) {
return false;
}

($i = 2; $i <= sqrt($number); $i++)


{ if ($number % $i == 0)
{ return false;
}}
return
true;
}

$number = 17; if
(isPrime($number)) {
echo "$number is a prime number.";
} else { echo "$number is NOT a prime
number.";
}
?>
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q20. Write a PHP script


Program:-
<?php

Output:-

Q16. print the Fibonacci series up to a given number.

function printFibonacciUpTo($limit) {
$a = 0;
$b = 1; echo "Fibonacci series up to
$limit:\n";
while ($a <= $limit) {
echo $a . " ";
$next = $a + $b;
$a = $b;
$b = $next;
}

$limit = 100;
printFibonacciUpTo($limit);
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q21. Write a PHP script


Program:-
<?php

?>

Output:-

to check whether a year is a leap year.

function isLeapYear($year) {
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
return true;
} else
{ return
false;
}} $year = 2024; if

(isLeapYear($year)) {

echo "$year is a leap year.";


NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q22. Write a PHP script


Program:-
<?php

} else { echo "$year is NOT a leap


year.";
}

?>

Output:-

Q18. implement a simple calculator using switch case.

function calculator($num1, $num2, $operator)


{ switch ($operator) { case '+':
return $num1 + $num2;
case '-':
return $num1 -
$num2; case '*': case 'x':
case 'X': return $num1 *
$num2; case '/':
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q23. Write a PHP script


Program:-
<?php

if ($num2 == 0) {
return "Error: Division by zero!";

} return $num1 / $num2;


default:
return "Invalid operator!";
} }

$num1 = 10;
$num2 = 5;
$operator = '*';
$result = calculator($num1, $num2, $operator);
echo "Result: $num1 $operator $num2 = $result";
?>
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Q19. Write a PHP count the occurrences of a specific word in a string.

Program:-
<?php
function countWordOccurrences($string, $word) {
$string = strtolower($string);
$word = strtolower($word);
$words = explode(' ', $string);
$count = 0; foreach
($words as $w) {
// Remove punctuation (optional)
$cleaned = preg_replace('/[^a-z0-9]/', '',
$w); if ($cleaned === $word) { $count++;
} } return
$count;
}
$text = "The quick brown fox jumps over the lazy dog. The dog was not amused.";
$targetWord = "the";
echo "The word '$targetWord' appears " . countWordOccurrences($text, $targetWord) . "
time(s).";
?>
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Output:-

Q20. Write a PHP script to reverse a number.

Program:-
<?php
function reverseNumber($number) {
$reversed = 0;
$isNegative = $number < 0;
$number = abs($number);

while ($number > 0) {


$digit = $number % 10;
$reversed = ($reversed * 10) + $digit;
$number = (int)($number / 10);
}

return $isNegative ? -$reversed : $reversed;


}

$num = 12345; echo "Original


number: $num\n";

echo "Reversed number: " . reverseNumber($num); ?


>
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025

Output:-

You might also like