PHP Assignment 1


1. Write a script for finding out whether it is an odd number or even number and echo on the screen.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the number <input type=”text” name=”num”>
<input type=”submit” name=”btn”><br>

<?php
if(isset($_POST[‘btn’]))
{
$val=$_POST[‘num’];
if($val%2==0)
{
echo “Number is even”;
}
else
{
echo “Number is odd”;
}
}
?>

</form>

</body>
</html>

 

 


2. Write a PHP Script to take 2 variables with specific numeric value of your choice and Perform the following operations: Addition, Subtraction, Multiplication and Division using switch case. Also echo out the Result on the screen.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the Value 1:<input type=”text” name=”num1″><br>
Enter the Value 2:<input type=”text” name=”num2″><br>

1.Add<br>
2.Sub<br>
3.Mul<br>
4.Div<br>
Enter your choice: <input type=”text” name=”val”>
<input type=”submit” name=”btn” value=”press”><br><br>

<?php
if(isset($_POST[‘btn’]))
{
$v=$_POST[‘val’];
$n1=$_POST[‘num1’];
$n2=$_POST[‘num2’];

switch($v)
{

case 1: echo “Addition:”.($n1+$n2); break;
case 2: echo “Subtraction:”.($n1-$n2); break;
case 3: echo “Multiplication:”.($n1*$n2); break;
case 4: echo “Divition:”.($n1/$n2); break;
default: echo “Enter proper choice”;

}
}
?>

</form>

</body>
</html>

 

 


3. Write a PHP Script to declare 2 variables with specific numeric value of your choice and find out the greater number between the two(Use ternary operator). If the numbers are equal, the respective message must appear on the screen.

Ans:

<html>
<body>

<form action=”” method=”post”>

Enter the Value 1:<input type=”text” name=”num1″><br>
Enter the Value 2:<input type=”text” name=”num2″>
<input type=”submit” name=”btn” value=”press”><br><br>
<?php
if(isset($_POST[‘btn’]))
{

$n1=$_POST[‘num1’];
$n2=$_POST[‘num2’];

if($n1>$n2)
{
echo “value 1 is greater”;
}
else if($n2>$n1)
{
echo “value 2 is greater”;
}
else
{
echo “Both are equal”;
}

}
?>

</form>

</body>
</html>

 


4.Write a PHP Script to check if the string is palindrome

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the Number: <input type=”text” name=”txt”>
<input type=”submit” name=”btn”><br>

<?php
if(isset($_POST[‘btn’]))
{

$n=$_POST[‘txt’];
$original= $n;
$rev=0;

while($n != 0 )
{
$remainder = $n%10;
$rev=$rev*10 + $remainder;
$n=(int)($n/10);
}

if ($original == $rev)
echo “$original is a palindrome”;
else
echo “$original is not a palindrome”;

}
?>
</form>

</body>
</html>

 


5. Write a PHP Script to Calculate the Sum of Odd & Even Numbers.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the Number: <input type=”text” name=”txt”>
<input type=”submit” name=”btn”><br>
</form>

<?php
if(isset($_POST[‘txt’]))
{

$num=$_POST[‘txt’];
$even=””;
$odd=””;

for($i=1;$i<=$num;$i++)
{
if($i%2 == 0)
{
$even=$even+$i;
}
else
{
$odd=$odd+$i;
}
}

echo “Sum of Even : “.$even.”<br>”;
echo “Sum of Odd : “.$odd.”<br>”;

}
?>

</body>
</html>

 


6. Write a PHP Script to Check if a given Integer is Positive or Negative.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the number <input type=”text” name=”num”>
<input type=”submit” name=”btn”><br>

<?php
if(isset($_POST[‘btn’]))
{

$val=$_POST[‘num’];
if($val>0)
{
echo “Number is possitive”;
}
else
{
echo “Number is nagative”;
}

}
?>
</form>

</body>
</html>

 


7. Write a PHP Script to Read Two Integers M and N & Swap their Values.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the Value 1:<input type=”text” name=”num1″><br>
Enter the Value 2:<input type=”text” name=”num2″>
<input type=”submit” name=”btn” value=”press”><br><br>

<?php
if(isset($_POST[‘btn’]))
{

$n1=$_POST[‘num1’];
$n2=$_POST[‘num2’];

$temp=$n1;
$n1=$n2;
$n2=$temp;

echo “value 1:”.$n1.”<br>”;
echo “value 2:”.$n2;

}
?>

</form>

</body>
</html>

 


8. Write a PHP Script to Accept two Integers and Check if they are Equal.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the Value 1:<input type=”text” name=”num1″><br>
Enter the Value 2:<input type=”text” name=”num2″>
<input type=”submit” name=”btn” value=”press”><br><br>

<?php
if(isset($_POST[‘btn’]))
{

$n1=$_POST[‘num1’];
$n2=$_POST[‘num2’];

if($n1=$n2)
{
echo “equal”;
}
else
{
echo “not equal”;
}

}
?>

</form>

</body>
</html>

 


9. Write a PHP Script to check if the given year is leap year or not and display message.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the Value 1:<input type=”text” name=”num1″><br>
Enter the Value 2:<input type=”text” name=”num2″>
<input type=”submit” name=”btn” value=”press”><br><br>

<?php
if(isset($_POST[‘btn’]))
{

$n1=$_POST[‘num1’];
$n2=$_POST[‘num2’];

if($n1=$n2)
{
echo “equal”;
}
else
{
echo “not equal”;
}

}
?>

</form>

</body>
</html>

 


10. Write a PHP Script to generate following series n = 5.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the number <input type=”text” name=”num”>
<input type=”submit” name=”btn”><br>

<?php
if(isset($_POST[‘btn’]))
{

$n=$_POST[‘num’];
$k=1;
for($i=0;$i<=$n;$i++)
{
for($j=0;$j<$i;$j++)
{
echo $k.” “;
$k++;
}
echo “<br>”;
}

}
?>
</form>

</body>
</html>

 


11. Write a PHP Script to generate factorial of 6 = 720.

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the number <input type=”text” name=”num”>
<input type=”submit” name=”btn”><br>

<?php
if(isset($_POST[‘btn’]))
{

$number=$_POST[‘num’];
$fact=1;
for($i=1;$i<=$number;$i++)
{
$fact=$fact*$i;
}
echo “fectorial:”.$number.”=”.$fact;

}
?>
</form>

</body>
</html>

 


12.Write a PHP Script to Generate the below multiplication table.

tab

Ans:

<html>
<body>

<table border=”1″>
<?php

for($i=1;$i<=5;$i++)
{
echo “<tr>”;
for($j=1;$j<=5;$j++)
{
echo “<td>$i*$j=”.($i*$j);
}
echo “</tr>”;
}

?>
</table>

</body>
</html>

 


13. Write the PHP script to generate the below table.
t2.png

Ans:

<html>
<body>

<table border=”1″>
<?php

$val=1;
for($i=1;$i<=10;$i++)
{
echo “<tr>”;
for($j=1;$j<=10;$j++)
{
echo “<td>”.$val;
$val++;
}
echo “</tr>”;
}

?>
</table>

</body>
</html>

 


14. Write the PHP script to generate the chess board.
chess

Ans:

<html>
<body>

<?php
echo “<table border=’.5′ width=’500px’>”;
for($i=0;$i<8;$i++)
{

echo “<tr>”;
for($j=0;$j<8;$j++)
{
$total=$i+$j;
if($total%2==0)
{
echo “<td height=’50px’ bgcolor=#FFFFFF>”;
}
else
{
echo “<td height=’50px’ bgcolor=#000000>”;
}

}
echo “</tr>”;
}

?>
</table>

</body>
</html>

 


15. Write a PHP Script to generate following Pattern.n=5

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the value<input type=”text” name=”val”>
<input type=”submit” name=”btn”>
</form>

<?php
if(isset($_POST[‘btn’]))
{

$v=$_POST[‘val’];
for($i=$v;$i>0;$i–)
{
for($j=$v;$j>=$i;$j–)
{
echo $j.” “;
}
echo “<br>”;
}

}
?>

</body>
</html>

 


16. Write a PHP Script to generate following Pattern. N=5

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the value<input type=”text” name=”val”>
<input type=”submit” name=”btn”>
</form>

<?php
if(isset($_POST[‘btn’]))
{

$v=$_POST[‘val’];
for($i=0;$i<$v;$i++)
{

for($j=0;$j<=$i;$j++)
{
echo “* “;
}
echo “<br>”;

}
for($i=$v-1;$i>0;$i–)
{

for($j=0;$j<$i;$j++)
{
echo “* “;
}
echo “<br>”;

}

}
?>

</body>
</html>


16. Write a PHP Script to generate following Pattern.N=5
Ans:

d5

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the value<input type=”text” name=”val”>
<input type=”submit” name=”btn”>
</form>

<?php
if(isset($_POST[‘btn’]))
{

$v=$_POST[‘val’];
for($i=0;$i<$v;$i++)
{

if($i==0 || $i==$v-1)
{

for($s=0;$s<$v;$s++)
{
echo “&nbsp;”;
}

for($s=0;$s<$v-2;$s++)
{
echo “*”;
}

echo “<br>”;

}
else if($i==1 || $i==$v-2)
{

for($s=0;$s<$v-4;$s++)
{
echo “&nbsp;”;
}
echo “*”;
for($s=0;$s<$v+$v;$s++)
{
echo “&nbsp;”;
}
echo “*”.”<br>”;
}
else
{
echo “*”;
for($s=0;$s<$v+$v+$v;$s++)
{
echo “&nbsp;”;
}
echo “*”.”<br>”;

}

}

}
?>

</body>
</html>

 

 


17. Write a PHP Script to generate following Pattern.N=5
# # # # #
# * * * #
# * * * #
# * * * #
# # # # #

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the value<input type=”text” name=”val”>
<input type=”submit” name=”btn”>
</form>

<?php
if(isset($_POST[‘btn’]))
{

$v=$_POST[‘val’];
for($i=1;$i<=$v;$i++)
{

if($i==1 || $i==$v)
{

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

echo “# “;

}
echo “<br>”;

}
else
{

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

if($j==1 || $j==$v)
{
echo “# “;
}
else
{
echo “* “;
}

}
echo “<br>”;

}

}

}
?>

</body>
</html>

 

 


18. Write a PHP Script to generate following Pattern.N=5

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the value<input type=”text” name=”val”>
<input type=”submit” name=”btn”>
</form>

<?php
if(isset($_POST[‘btn’]))
{

$v=$_POST[‘val’];
for($i=1;$i<=$v;$i++)
{
for($s=$v;$s>=$i;$s–)
{
echo “&nbsp”;
}
for($j=1;$j<=$i;$j++)
{
echo ” $i “;
}
echo “<br>”;
}

}
?>

</body>
</html>

 


19. Write a PHP Script to first 10 prime numbers.

Ans:

<html>
<body>

<?php

$count = 0 ;
$number = 2 ;
while ($count < 10 )
{

$div_count=0;
for ( $i=1;$i<=$number;$i++)
{

if (($number%$i)==0)
{
$div_count++;
}

}
if ($div_count<3)
{

echo $number.” , “;
$count=$count+1;

}
$number=$number+1;

}

?>
</body>
</html>

 

 


20. Write a PHP Script to generate following Pattern.N=5
1
10
101
1010
10101

Ans:

<html>
<body>

<form action=”” method=”post”>
Enter the value<input type=”text” name=”val”>
<input type=”submit” name=”btn”>
</form>

<?php
if(isset($_POST[‘btn’]))
{

$v=$_POST[‘val’];
for($i = 1; $i <= $v; $i++)
{

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

if ($j % 2 == 1)
{
echo “1 “;
}
else
{
echo “0 “;
}

}
echo “<br>”;

}

}
?>

</body>
</html>