0% found this document useful (0 votes)
32 views10 pages

Assignment 22

The document contains a series of PHP programming assignments focusing on control structures and loops. It includes scripts for checking leap years, performing arithmetic operations, displaying prime and perfect numbers, reversing numbers, and identifying Armstrong numbers. Additionally, it features scripts for converting numbers to words, changing background colors based on the day of the week, and counting even and odd numbers.

Uploaded by

Sakshi Takwale
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)
32 views10 pages

Assignment 22

The document contains a series of PHP programming assignments focusing on control structures and loops. It includes scripts for checking leap years, performing arithmetic operations, displaying prime and perfect numbers, reversing numbers, and identifying Armstrong numbers. Additionally, it features scripts for converting numbers to words, changing background colors based on the day of the week, and counting even and odd numbers.

Uploaded by

Sakshi Takwale
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/ 10

Assignment 2:Control Structures and Loops

SET A

1. Write a PHP Script to check whether a year is a leap or not.

Solution:

<!DOCTYPE html>
<html>
<body>

<?php
$year = 2032;

if((0 == $year % 4) & (0 != $year % 100) | (0 == $year % 400))


{
echo "$year is a Leap Year.";
}

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

// Checking submit condition


if(isset($_POST['submit'])) {

// Taking first number from the


// form data to variable 'a'
$a = $_POST['num1'];

// Taking second number from the


// form data to a variable 'b'
$b = $_POST['num2'];

// Taking option from the form


// data to a variable 'ch'
$ch = $_POST['option'];

switch($ch) {
case 1:
// Execute addition operation
// when option 1 is given
$r = $a + $b;
echo " Addition of two numbers = " . $r ;
break;

case 2:

// Executing subtraction operation


// when option 2 is given
$r = $a - $b;
echo " Subtraction of two numbers= " . $r ;
break;

case 3:

// Executing multiplication operation


// when option 3 is given
$r = $a * $b;
echo " Multiplication of two numbers = " . $r ;
break;

case 4:

// Executing division operation


// when option 4 is given
$r = $a / $b;
echo " Division of two numbers = " . $r ;
break;

default:

// When 1 to 4 option is not given


// then this condition is executed
echo ("invalid option\n");
}
return 0;} ?> </body></html>
SET B:
1. Write a PHP Script to display prime numbers between 1 to 50.
Solution:

<?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>

4. Write a PHP Script to display Armstrong numbers between 1 to 500.


Solution:
<?php
for($i = 1; $i <= 1000; $i++) {
$sum = 0;
$qu = $i;
while($qu != 0) {
$remainder = $qu % 10;
$thrice = $remainder * $remainder * $remainder;
$sum += $thrice;
$qu = $qu / 10;
}
if ($sum == $i) {
echo "<p>$i is armstrong number</p>";
}
}
?>
SET C

1. Write a PHP script to display a number in words (Use Switch case)


e.g. 345–three four five

<?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

// program to count no of even and odd numbers


$even = 0;
$odd = 0;
$value = 20;

for ($value ; $value > 0 ; $value-- ) {


if ($value%2 == 0) {
$even++;
echo "Value: $value " . "is an Even Number" . "<br />";
} else {
$odd++;
echo "Value: $value " . "is an Odd Number" . "<br />";
}
}
echo "<br /><br />";
echo "Total Even & Odd numbers from 1 - $value:";
echo "<br />";
echo "Evens: $even" . "<br />";
echo "Odds: $odd";
?>

You might also like