Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

php (2)

The document contains a series of PHP programming exercises submitted by Manthan Surkar for a Web Technologies course. Each question includes a specific task, a PHP program to accomplish that task, and the expected output. The tasks range from basic operations like finding the sum of digits to more complex functions such as checking for prime numbers and implementing a simple calculator.

Uploaded by

ponima5849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

php (2)

The document contains a series of PHP programming exercises submitted by Manthan Surkar for a Web Technologies course. Each question includes a specific task, a PHP program to accomplish that task, and the expected output. The tasks range from basic operations like finding the sum of digits to more complex functions such as checking for prime numbers and implementing a simple calculator.

Uploaded by

ponima5849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

NAME: Manthan Surkar PRN No: ADT24MGTM0946

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


SUBJECT: Web Technologies DATE: 19/03/2025
Q1. Write a PHP program to find the sum of digits of a given number.
Program:-
<?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: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B 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: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q3. 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);
?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
Subject: Web Technologies DATE: 19/03/2025
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: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q5. 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).";
}

?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q7. Write 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.";
}
?>

Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q8. Write a PHP 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;
}

$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
Q9. 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
Q10. Write a PHP program to find the largest of three numbers.
Program:-
<?php
function findLargest($a, $b, $c) {
if ($a >= $b && $a >= $c) {
return $a;
} elseif ($b >= $a && $b >= $c) {
return $b;
} 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:-
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 check if a given string is a palindrome.

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
Q12. Write a PHP program to count the number of words in a string.

Program:-

<?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);
?>

Output:-
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 reverse a string without using built-in functions.

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

return $reversed;
}

$input = "Hello, PHP!";


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

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 calculate the factorial of a number using recursion.

Program:-
<?php
function factorial($n) {
// Base case
if ($n <= 1)
{ 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
Q15. Write a PHP program to check whether a number is prime or not.

Program:-
<?php
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.";
}
?>

Output:-
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 print the Fibonacci series up to a given number.

Program:-
<?php
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);
?>

Output:-
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 check whether a year is a leap year.

Program:-
<?php
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.";
} else {
echo "$year is NOT a leap year.";
}

?>

Output:-
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 implement a simple calculator using switch case.

Program:-
<?php
function calculator($num1, $num2, $operator) {
switch ($operator) {
case '+':
return $num1 + $num2;
case '-':
return $num1 - $num2;
case '*':
case 'x':
case 'X':
return $num1 * $num2;
case '/':
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 script to 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).";
?>

Output:-
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 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);


?>

Output:-

You might also like