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

Program 3 Php

Uploaded by

acharvitha896
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)
5 views

Program 3 Php

Uploaded by

acharvitha896
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/ 8

3(a) a.

Develop a PHP Program(s) to check given number is:


(i) Odd or even
<!DOCTYPE html>
<html>
<head>
<title>Odd or Even Checker</title>
</head>
<body>
<h1>Odd or Even Checker</h1>
<form method="Post" >
Enter a number: <input type="text" name="number" >
<br><br>
<input type="submit" name=”submit” value="Check">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$number = $_POST["number"];
if (is_numeric($number))
{if ($number % 2 == 0)
{
echo "$number is an even number";
}
else
{
echo "$number is an odd number";
}
}
else
{
echo "Enter a valid number";
}
}
?>
</body>
</html>

ii) Divisible by a given number (N)


<!DOCTYPE html>
<html>
<head>
<title>Divisibility Checker</title>
</head>
<body>
<h1>Divisibility Checker</h1>
<form method="Post">
Enter a number:<input type="text" name="number">
<br><br>
Enter a divisor: <input type="text" name="divisor">
<br><br>
<input type="submit" value="Check">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$number = $_POST["number"];
$divisor = $_POST["divisor"];

if (is_numeric($number) && is_numeric($divisor))


{
if ($divisor == 0)
{
echo "Cannot divide by zero";
}
else
{
if ($number % $divisor == 0)
{
echo "$number is divisible by $divisor";
}
else
{
echo "$number is not divisible by $divisor";
}
}
}
else
{
echo "Enter valid numbers";
}
}
?>
</body>
</html>

(iii) Square of a number


<!DOCTYPE html>
<html>
<head>
<title>Square Calculator</title>
</head>
<body>
<h1>Square Calculator</h1>
<form method="Post">
Enter a number:<input type="text" name="number">
<br><br>
<input type="submit" value="Calculate Square">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$number = $_POST["number"];
if (is_numeric($number))
{
$square = $number * $number;
echo "The square of $number is $square";
}
else
{
echo "Enter a valid number";
}
}
?>
</body>
</html>

3(b) . Develop a PHP Program to compute the roots of a quadratic


equation by accepting the coefficients. Print the appropriate
messages.
<!DOCTYPE html>
<html>
<head>
<title>Quadratic Equation Solver</title>
</head>
<body>
<h1>Quadratic Equation Solver</h1>
<form method="post">
Enter the coefficient a: <input type="text" name="a" >
<br><br>
Enter the coefficient b: <input type="text" name="b" >
<br><br>
Enter the coefficient c: <input type="text" name="c" >
<br><br>
<input type="submit" value="Calculate Roots">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$a = $_POST["a"];
$b = $_POST["b"];
$c = $_POST["c"];

// Check if inputs are numeric


if (is_numeric($a) && is_numeric($b) && is_numeric($c)) {
$discriminant = ($b * $b) - (4 * $a * $c);

// Case 1: Real and different roots


if ($discriminant > 0) {
$root1 = (-$b + sqrt($discriminant)) / (2 * $a);
$root2 = (-$b - sqrt($discriminant)) / (2 * $a);
echo "The roots of the quadratic equation are real and
different.<br>";
echo "Root 1: $root1<br>";
echo "Root 2: $root2<br>";

// Case 2: Real and equal roots


} elseif ($discriminant == 0) {
$root = -$b / (2 * $a);
echo "The root of the quadratic equation is real and
equal.<br>";
echo "Root: $root<br>";

// Case 3: Complex roots


} else {
$realPart = -$b / (2 * $a);
$imaginaryPart = sqrt(-$discriminant) / (2 * $a);
echo "The roots of the quadratic equation are complex and
different.<br>";
echo "Root 1: " . $realPart . " + " . $imaginaryPart . "i<br>";
echo "Root 2: " . $realPart . " - " . $imaginaryPart . "i<br>";
}
} else {
echo "Please enter valid numeric coefficients.";
}
}
?>

</body>
</html>

You might also like