Assignment 22
Assignment 22
SET A
Solution:
<!DOCTYPE html>
<html>
<body>
<?php
$year = 2032;
else
{
echo "$year is not a Leap Year.";
}
?>
</body>
</html>
2. Write a PHP Script which will perform the Addition, Subtraction,
Multiplication, and Division of two numbers as per the choice. (Use Switch
Case)
Solution:
<!DOCTYPE html>
<html>
<head>
<title>GFG</title>
</head>
<body><center>
<h1>
ARITHMETIC OPERATIONS DEMO USING
SWITCH CASE IN PHP
</h1>
<h3>Option-1 = Addition</h3>
<h3>Option-2 = Subtraction</h3>
<h3>Option-3 = Multiplication</h3>
<h3>Option-4 = Division</h3>
<form method="post">
<table border="0">
<tr>
<!-- Taking value 1 in an text box -->
<td> <input type="text" name="num1"
value="" placeholder="Enter value 1"/>
</td>
</tr>
<tr>
<!-- Taking value 1 in an text box -->
<td> <input type="text" name="num2" value=""
placeholder="Enter value 2"/>
</td>
</tr>
<tr>
<!-- Taking option in an text box -->
<td> <input type="text" name="option" value=""
placeholder="Enter option 1-4 only"/>
</td>
</tr>
<tr>
<td> <input type="submit" name="submit"
value="Submit"/>
</td>
</tr>
</table>
</form>
</center>
<?php
switch($ch) {
case 1:
// Execute addition operation
// when option 1 is given
$r = $a + $b;
echo " Addition of two numbers = " . $r ;
break;
case 2:
case 3:
case 4:
default:
<?php
$count = 0;
$num = 2;
while ($count < 15 )
{
$div_count=0;
for ( $i=1; $i<=$num; $i++)
{
if (($num%$i)==0)
{
$div_count++;
}
}
if ($div_count<3)
{
echo $num." , ";
$count=$count+1;
}
$num=$num+1;
}
?>
2. Write a PHP Script to display a perfect numbers between 1 to100.
Solution:
<html>
<body>
<h2>Perfect, Abundant or Deficient</h2>
<form action="" method="post">
Enter the number:
<input type="text" name="number" />
<input type="submit" />
</form>
</body>
</html>
<?php
if ($_POST) {
$no = $_POST['number'];
$sum = 0;
for ($i = 1; $i < $no; $i++) {
if ($no % $i == 0)
$sum = $sum + $i;
}
if ($sum == $no)
echo "Perfect Number";
else if ($sum > $no)
echo "Abundant Number";
else
echo "Deficient Number";
}
?>
3. Write a PHP Script to display the reverse of a number. E.g. 607 =>706
Solution:
<!DOCTYPE html>
<html>
<body>
<?php
$num = 123456789;
$x = 0;
$n =$num;
while(floor($num))
{
$mod = $num%10;
$x = $x * 10 + $mod;
$num = $num/10;
}
echo "Reverse of $n is $x.";
?>
</body>
</html>
<?php
function word_digit($word) {
$warr = explode(';',$word);
$result = '';
foreach($warr as $value){
switch(trim($value)){
case 'zero':
$result .= '0';
break;
case 'one':
$result .= '1';
break;
case 'two':
$result .= '2';
break;
case 'three':
$result .= '3';
break;
case 'four':
$result .= '4';
break;
case 'five':
$result .= '5';
break;
case 'six':
$result .= '6';
break;
case 'seven':
$result .= '7';
break;
case 'eight':
$result .= '8';
break;
case 'nine':
$result .= '9';
break;
}
}
return $result;
}
echo word_digit("zero;three;five;six;eight;one")."\n";
echo word_digit("seven;zero;one")."\n";
?>
2. Write a PHP script to change the background color of the browser using
a switch statement according to the day of the week.
Solution:
<?php
$day=date("l");
switch($day)
{
case 'Monday':
$bg_color = "red";
break;
case 'Tuesday':
$bg_color = "blue";
break;
case 'Wednesday':
$bg_color = "blue";
break;
case 'Thursday':
$bg_color = "gray";
break;
case 'Friday':
$bg_color = "yellow";
break;
case 'Saturday':
$bg_color = "green";
break;
case 'Sunday':
default:
$bg_color = "black";
break;
}
echo "
Welcome to my Homepage
";
?>
3. Write a PHP script to count the total number of even and odd numbers
between1 to 1000.
<?php