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

PHP Programs WT

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)
6 views

PHP Programs WT

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/ 11

1)The PHP program to find the largest number among three numbers:

Algorithm:
1. Start
2. Initialize three variables: a, b, and c with values (e.g., a=10, b=20, c=30).
3. Check if a > b:
4. If true, check if a > c:
a. If true, print "a is the largest number."
b. Otherwise, print "c is the largest number."
5. Else (if a <= b), check if b > c:
1. If true, print "b is the largest number."
2. Otherwise, print "c is the largest number."
6. End
Program:
<html>
<head>
<title>Selection Demo</title>
</head>
<body>
<?php
print '<h2>Selection Statement</h2>';

$a = 10;
$b = 20;
$c = 30;

if ($a > $b) {


if ($a > $c) {
print "<b><i>a is the largest number</i></b>";
} else {
print "<b><i>c is the largest number</i></b>";
}
} else {
if ($b > $c) {
print "<b><i>b is the largest number</i></b>";
} else {
print "<b><i>c is the largest number</i></b>";
}
}
?>
</body>
</html>

Output:

2)The PHP script to compute the sum and average of N numbers:

Algorithm:
1. Start
2. Initialize a variable sum to 0.
3. Loop through numbers from 1 to 10:
4. Add the current number to sum.
5. Calculate the average:
6. Divide sum by 10 and store the result in avg.
7. Print the sum and average.
8. End
Program:
<html>
<head>
<title>Sum and Average</title>
</head>
<body>
<center>
<?php
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
}

$avg = $sum / 10;

print "The Sum is: $sum";


print "<br/>";
print "The Average is: $avg";
?>
</center>
</body>
</html>

Output:

3)The PHP program to print whether the current year is a leap year or not:
Algorithm:
1. Start
2. Initialize a variable year with a value (e.g., 2016).
3. Check if year is divisible by 4:
4. If true, print "Year is a leap year."
5. Otherwise, print "Year is not a leap year."
6. End
Program:
<html>
<head>
<title>Leap Year Demo</title>
</head>
<body>
<?php
$year = 2016;

print "<br/>";
if ($year % 4 == 0) {
printf("Year %d is a leap year", $year);
} else {
printf("Year %d is not a leap year", $year);
}
?>
</body>
</html>

Output:

4)The PHP program to print whether a given number is odd or even:


Algorithm:
1. Start
2. Loop from i = 1 to 10:
3. Assign num = i.
4. Check if num % 2 == 1:
5. If true, print "Number is Odd."
6. Otherwise, print "Number is Even."
7. Print a newline after each result.
8. End
Program:
<html>
<head>
<title>Even Odd Demo</title>
</head>
<body>
<?php
for ($i = 1; $i <= 10; $i++) {
$num = $i;

if ($num % 2 == 1) {
printf("Number %d is Odd", $num);
} else {
printf("Number %d is Even", $num);
}

print "<br/>";
}
?>
</body>
</html>

Output:

5)The PHP script to compute the factorial of 'n' using a for loop:
Algorithm:
1. Start
2. Initialize a variable n with the number (e.g., 5).
3. Set factorial = 1 to hold the product.
4. Loop from i = n down to 1:
5. Multiply factorial by i.
6. Print the value of factorial as the factorial of n.
7. End
Program:
<html>
<head>
<title>Factorial Program</title>
</head>
<body>
<?php
$n = 5;
$factorial = 1;

for ($i = $n; $i >= 1; $i--) {


$factorial *= $i;
}

printf("Factorial of %d is %d", $n, $factorial);


?>
</body>
</html>

Output:
6)The PHP script to compute the square root, square, cube, and quad of 10 numbers:
Algorithm:
1. Start
2. Print the table header with columns: "num", "Sqr", "Cube", and "Quad".
3. Loop through numbers from 1 to 10:
4. For each count:
1. Calculate the square: sq = count * count.
2. Calculate the cube: cube = count * count * count.
3. Calculate the quadruple: quad = count * count * count * count.
5. Print a table row displaying count, sq, cube, and quad.
6. End
Program:
<html>
<head>
<title>SQUARE CUBE QUAD DEMO</title>
</head>
<body>
<?php
print "<b>Table</b><br/>";
print "<table border='1'>";
print "<tr><th>num</th><th>Sqr</th><th>Cube</th><th>Quad</th></tr>";

for ($count = 1; $count <= 10; $count++) {


$sq = $count * $count;
$cube = $count * $count * $count;
$quad = $count * $count * $count * $count;

print "<tr><td>$count</td><td>$sq</td><td>$cube</td><td>$quad</td></tr>";
}

print "</table>";
?>
</body>
</html>
Output:

7)The PHP Program To Print A Message Based On Today's Date Using Switch And If
Structures

Algorithm:
1. Start
2. Get today's date (day) using date("d").
3. Check if the day is less than 3 or greater than 10:
If true, print "No Event!!!".
4. Otherwise, check if the day is between 4 and 9:
If true, print "No Event!!!".
5. Else (the day is either 3 or 10):
If day is 3, print "Dentist Appointment".
If day is 10, print “Go to Conference”.
6. End
Program:
<!DOCTYPE html>
<html>
<body>
<?php
echo “Today date is “ . date(“d/m/y”) . “<br>”;

if ((date(“d”) < 3) || (date(“d”) > 10)) {


echo “No Event!!!”;
} else if ((date(“d”) > 3) && (date(“d”) < 10)) {
echo “No Event!!!”;
} else {
switch (date(“d”)) {
case 3:
echo “Dentist Appointment”;
break;
case 10:
echo “Go to Conference”;
break;
}
}
?>
</body>
</html>

Output:
8)The Php Code To Display The Pattern
1
01
101
0101
10101

Algorithm:
1. Start
2. Loop from i = 1 to 5 (for each row):
3. Loop from j = 1 to i (for each column in that row):
a) Check if the sum of i + j is even:
i) If true, print 0.
ii) Otherwise, print 1.
4. After completing the inner loop, print a newline.
5. End
Program:
<html>
<head>
<title>Pattern Demo</title>
</head>
<body>
<?php
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
if (($i + $j) % 2 == 0) {
printf("0");
} else {
printf("1");
}
}
print "<br/>";
}
?>
</body>
</html>

Output:

You might also like