0% found this document useful (0 votes)
12 views5 pages

PHP Labpro 3,4

Hsbdbdbdbbdbdbndbdbddnndbdbdndbdbbdbdndndndndndndnndndndndndndndndndndndndnndndndndndndnddnnd

Uploaded by

vihitog478
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)
12 views5 pages

PHP Labpro 3,4

Hsbdbdbdbbdbdbndbdbddnndbdbdndbdbbdbdndndndndndndnndndndndndndndndndndndndnndndndndndndnddnnd

Uploaded by

vihitog478
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

3.

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


(i) Odd or even

<?php
function checkOddEven($number) {

if ($number % 2 == 0) {
echo "{$number} is an even number.";

} else {
echo "{$number} is an odd number.";

}
}

$numberToCheck = 5;
checkOddEven($numberToCheck);

?>
Output

5 is an odd number.

(ii)Divisible by a given number (N)


<?php

function isDivisible($number, $divisor) {


if ($divisor == 0) {
return "Cannot divide by zero!";

}
if ($number % $divisor == 0) {

return "$number is divisible by $divisor";


} else {

return "$number is not divisible by $divisor";


}}

$givenNumber = 15;
$divisorNumber = 3;
$result = isDivisible($givenNumber, $divisorNumber);
echo $result;

?>
Output

15 is divisible by 3

(iii)Square of a another number


<?php

function isSquare($num) {
$squareRoot = sqrt($num);

if ($squareRoot == (int)$squareRoot) {
return true;

} else {
return false;

}
}

$numberToCheck = 16;
if (isSquare($numberToCheck)) {

echo "$numberToCheck is the square of another number.";


} else {
echo "$numberToCheck is not the square of another number.";

}
?>

Output
16 is the square of another number.

3 b. Develop a PHP Program to compute the roots of a quadratic equation by accepting


the coefficients. Print the appropriate messages.
<?php
function roots($a,$b,$c)
{
$D = (($b*$b) – (4*$a*$c));

if ($D >= 0){


$x1 = (-$b + sqrt($D))/(2*$a);
$x2 = (-$b - sqrt($D))/(2*$a);
echo "Roots are: $x1, $x2 ";
} else {
$x1 = -$b/(2*$a);
$x2 = sqrt(-$D)/(2*$a);
echo "Roots are: $x1 ± $x2 i ";
}
}
echo "Equation is x*x+5x+4=0";
roots(1,5,4);
echo "\n Equation is x*x+4x+5=0";
roots(1,4,5);
?>

Output
Equation is x*x+5x+4=0
Roots are: -1, -4

Equation is x*x+4x+5=0
Roots are: -2 ± 1 i
4 a. Develop a PHP program to find the square root of a number by using the newton’s
algorithm.

<?php
function squareRoot($number, $tolerance=1e-10) {

$x = $number/2;

while (abs($x*$x-$number)>$tolerance){
$x=0.5*($x+($number/$x));

}
return $x;

}
$number = 25;

$result = squareRoot($number);
echo "Square root of $number is: $result";

?>
Output

Square root of 25 is: 5

4 b. Develop a PHP program to generate Floyd’s triangle.


<?php

function generateFloydsTriangle($rows)
{

$number = 1;
echo "Floyd's Triangle with $rows rows: <br>";
for ($i = 1; $i <= $rows; $i++) {

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


echo $number . " ";

$number++;
}
echo "<br>";

}
}

$rows = 5;
generateFloydsTriangle($rows);

?>
Output

Floyd's Triangle with 5 rows:


1
23
456
7 8 9 10
11 12 13 14 15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy