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

Lab Programs- 1-5

The document contains five PHP lab programs demonstrating various functionalities: checking if a number is odd or even, finding the maximum of three numbers, swapping two numbers, calculating the factorial of a number, and checking if a number is a palindrome. Each program includes HTML forms for user input and PHP scripts for processing the input and displaying results. The scripts validate user input and provide appropriate feedback for invalid entries.

Uploaded by

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

Lab Programs- 1-5

The document contains five PHP lab programs demonstrating various functionalities: checking if a number is odd or even, finding the maximum of three numbers, swapping two numbers, calculating the factorial of a number, and checking if a number is a palindrome. Each program includes HTML forms for user input and PHP scripts for processing the input and displaying results. The scripts validate user input and provide appropriate feedback for invalid entries.

Uploaded by

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

Lab Program 1

Write a PHP script to find odd or even number from a given number.
<html>

<head>
<title>ODD or EVEN</title>
</head>
<body>

<h2>PHP script to find if a given number is ODD or EVEN</h2>

<form action="" method="post">


<input type="text" name="num" placeholder="Enter a number" />
<input type="submit" value="Check" />
</form>

<?php
// Check if form is submitted
if($_POST)
{
$num = $_POST['num']; // Retrieve user input

// Validate if the input is a numeric value


if(!is_numeric($num))
{
echo "Invalid input! Please enter a valid number.";
return; // Stop further execution
}

// Check if the number is even or odd using modulus operator


if($num % 2 == 0)
{
echo "The number $num is an even number.";
}
else
{
echo "The number $num is an odd number.";
}
}
?>

</body>
</html>
Lab Program 2
Write a PHP script to find maximum of three numbers.
<html>
<head>
<title>Maximum Number Finder</title>
</head>
<body>
<h2>PHP Script To Find Maximum of Three Numbers</h2>
<form method="post">
<input type="text" name="num1" value="" placeholder="Enter 1st number"/>
<br> <br>
<input type="text" name="num2" value="" placeholder="Enter 2nd number"/>
<br> <br>
<input type="text" name="num3" value="" placeholder="Enter 3rd number"/>
<br> <br>
<input type="submit" name="submit" value="Submit"/>
</form>

<?php
if($_POST) { // Check if the form is submitted
$a = $_POST['num1']; // Get the first number from the form
$b = $_POST['num2']; // Get the second number from the form
$c = $_POST['num3']; // Get the third number from the form

// Check if inputs are numeric


if(!is_numeric($a) || !is_numeric($b) || !is_numeric($c)) {
echo "Please enter valid numbers.";
return;
}

// Compare the three numbers to find the largest


if ($a > $b && $a > $c) {
echo "$a is the largest number.";
} else if ($b > $c && $b > $a) {
echo "$b is the largest number.";
} else {
echo "$c is the largest number.";
}
}
?>
</body>
</html>
Lab Program 3
Write a PHP script to swap two numbers.
<?php
echo "<h2> PHP Script To Swap Two Numbers </h2>";
$a = 10;
$b = 20;
echo "Before swapping: <br> \$a = $a <br> \$b = $b <br><br>";
$temp = $a;
$a = $b;
$b = $temp;
echo "After swapping: <br> \$a = $a <br> \$b = $b";
?>
Lab Program 4
Write a PHP script to find the factorial of a number.
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h2>PHP Script to Find the Factorial of a Number</h2>

<form method="post">
Enter the Number:
<input type="number" name="number" id="number"> <br> <br>

<input type="submit" name="submit" value="Submit" />


</form>

<?php
if($_POST){ // Check if the form is submitted
$fact = 1;
//getting value from input text box 'number'
$number = $_POST['number'];
echo "Factorial of $number:";
//start loop
for ($i = 1; $i <= $number; $i++){
$fact = $fact * $i;
}
echo $fact . "<br>";
}
?>

</body>
</html>
Lab Program 5
Write a PHP script to check whether given number is palindrome or
not.
<html>
<head>
<title>Palindrome Checker</title>
</head>
<body>
<h2>PHP Script to Check Whether the Given Number is a Palindrome or Not</h2>

<form method="post">
Enter a Number:
<input type="text" name="num" id="number"/> <br> <br>
<input type="submit" name="submit" value="Check"/>
</form>

<?php
if($_POST){ // Check if the form is submitted
//get the value from form
$num = $_POST['num'];
//reversing the number
$reverse = strrev($num);

//checking if the number and reverse is equal


if($num == $reverse){
echo "The number $num is a Palindrome";
} else {
echo "The number $num is not a Palindrome";
}
}
?>

</body>
</html>

You might also like