PRACTICE PROGRAM :-
1. Write a PHP Script to display a maximum of two numbers.
ANS:
HTML CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="Q1.php">
<h1>finding maximum of two number!</h1>
Enter first number: <input type="text" name="first" ><br>
<br>
Enter second number: <input type="text" name="second"><br>
<br>
<button>max is!</button>
</form>
</body>
</html>
PHP SCRIPT:
<?php
$n1 = $_GET["first"];
$n2 = $_GET["second"];
if ($n1>$n2) {
echo("\n $n1 is greater than $n2!");
} else {
echo("\n $n2 is greater than $n1!");
}
?>
-----------------------------------------------------------------------------------------------------------------------------
2. Write a PHP Script to check whether a number is positive or negative.
ANS:
HTML CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>check number is positive or negative!</h1>
<form action="Q2.php">
Enter a number: <input type="text" name="num"><br>
<br>
<button>click to check!</button>
</form>
</body>
</html>
PHP CODE:
<?php
$n = $_GET["num"];
if ($n>0) {
echo("\n the $n is positive number!");
} else {
echo("\n the $n is negative number!");
}
?>
-----------------------------------------------------------------------------------------------------------------------------
3. Write a PHP Script to display a Multiplication table of a number.
ANS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>making multiplication table of given number!</h1>
<form action="Q3.php">
Enter a number: <input type="text" name="num"><br>
<br>
<button>click to display!</button>
</form>
</body>
</html>
PHP CODE:
<?php
$n = $_GET["num"];
$i=1;
while ($i<=10) {
$t=$n*$i;
echo("$t"."<br>");
$i++;
}
?>
-----------------------------------------------------------------------------------------------------------------------------
SET A:-
1. Write a PHP Script to check whether a year is a leap or not.
ANS:
HTML CODE AND PHP CODE:
<html>
<body>
<form action="Q1.php" method="post">
Enter a year:<input type="text" name="ye"><br>
<button>click me!</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$y=$_POST['ye'];
if ($y%4==0) {
echo"$y is a leap year!";
}
else {
echo"$y is not leap year!";
}
}
?>
-----------------------------------------------------------------------------------------------------------------------------
2. Write a PHP Script which will perform the Addition, Subtraction, Multiplication, and
Division of two numbers as per the choice. (Use Switch Case)
ANS:
HTML AND PHP CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="main">
<form action="Q2.php" method="post">
<u>Enter first number</u>: <input type="text" name="first" ><br>
<br>
<u>Enter second number</u>: <input type="text" name="second"><br>
<br>
<b>choice</b>: <select name="selection">
<option value="1">Addition</option>
<option value="2">Subtraction</option>
<option value="3">Multiplication</option>
<option value="4">Division</option>
</select><br>
<br>
<button>click to calculate!</button><br>
<br>
</form>
</div>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$a=$_POST["first"];
$b=$_POST["second"];
$c=$_POST["selection"];
$ans;
switch ($c) {
case '1':
$ans=$a+$b;
echo "Addition of $a and $b = $ans";
break;
case '2':
$ans=$a-$b;
echo"Subtraction of $a and $b = $ans";
break;
case '3':
$ans=$a*$b;
echo"Multiplication of $a and $b = $ans";
break;
case '4':
$ans=$a/$b;
echo"Division of $a and $b = $ans";
break;
default:
echo"NO one choice selected!";
break;
}
}
?>
-----------------------------------------------------------------------------------------------------------------------------
3. Write a PHP Script to display the grade of the student according to percentage. Use the
following conditions:
Percentage <40 => Grade=”Fail”
Percentage >= 40 and Percentage <=50 => Grade= “Pass Class”
Percentage >=50 and Percentage <=60 => Grade= “Higher Second Class”
Percentage >60 and Percentage <=70 => Grade= “First Class”
Percentage >70 => Grade= “First Class with Distinction”.
ANS:
HTML AND PHP CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="Q3.php" method="get">
<u>Enter percentage</u>: <input type="text" name="per"><br>
<br>
<input type="submit" name="submit">
</form>
<?php
$g="";
if ($_SERVER["REQUEST_METHOD"]=="GET") {
if (isset($_GET['submit'])) {
$per=$_GET["per"];
if ($per>70&&$per<=100) {
$g="A+";
}elseif ($per>60&&$per<=70) {
$g="A";
}elseif ($per>=50&&$per<=60) {
$g="B";
}
elseif ($per>=40&&$per<=50) {
$g="C";
}
elseif ($per<40) {
$g="F";
}
else{
$g="percentage<=100";
}
}
}
?>
<u>Grade is</u>: <input type="text" name="grade" value="<?php echo$g;?>"><br>
<br>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------
SET B:
1. Write a PHP Script to display prime numbers between 1 to 50.
ANS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Enter range to display prime numbers!</h1>
<form action="Q1.php" method="POST">
Enter starting of range: <input type="text" name="s">
<br>
Enter End of range: <input type="text" name="E" ><br>
<br>
<button>click</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$start= $_POST["s"];
$end=$_POST["E"];
for ($i=$start; $i <= $end ; $i++) {
$flag=0;
for ($j=2; $j < $i ; $j++) {
if ((int)$i%$j==0) {
$flag=1;
break;
}
}
if ($flag==0) {
echo$i."<br>";
}
}
}
?>
-----------------------------------------------------------------------------------------------------------------------------
2. Write a PHP Script to display a perfect numbers between 1 to100.
ANS:
PHP CODE:
<?php
echo"<h1>This program to print perfect numbers between 1 to 100!</h1>";
$s=0;
for ($i=1; $i < 100; $i++) {
$j=1;
while ($j < $i) {
if ($i%$j==0) {
$s=$s+$j;
}
$j++;
}
if ($s==$i) {
echo"<strong><big>$i</big></strong>"."<br>";
}
$s=0;
}
?>
-----------------------------------------------------------------------------------------------------------------------------
3. Write a PHP Script to display the reverse of a number. E.g. 607 =>706.
ANS:
HTML CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Give a number for reverse!</h1>
<form action="Q3.php">
Enter a number: <input type="text" name="num"><br>
<br>
<button>click to reverse</button>
</form>
</body>
</html>
PHP CODE:
<?php
$n = $_GET["num"];
$rev=0;
$temp = $n;
while ((int)$n>0) {
$d = $n%10;
$rev =($rev*10)+$d;
$n=$n/10;
}
echo("the reverse of $temp is ".$rev);
?>
-----------------------------------------------------------------------------------------------------------------------------
SET C:
1. Write a PHP script to display a number in words (Use Switch case)
e.g. 345–three four five.
ANS:
HTML AND PHP CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>PHP script to convert number into word!</h2>
<form action="Q1.php" method="post">
<b>Enter a number:</b><input type="number" name="num"><br>
<br>
<button type="submit">click</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$n=$_POST["num"];
$rev=0;
while ((int)$n>0) {
$d=$n%10;
$rev=$rev*10+$d;
$n=$n/10;
}
while ((int)$rev>0) {
$d=$rev%10;
switch ((int)$d) {
case '0':
echo"\tZERO";
break;
case '1':
echo"\tONE";
break;
case '2':
echo"\tTWO";
break;
case '3':
echo"\tTHREE";
break;
case '4':
echo"\tFOUR";
break;
case '5':
echo"\tFIVE";
break;
case '6':
echo"\tSIX";
break;
case '7':
echo"\tSEVEN";
break;
case '8':
echo"\tEIGHT";
break;
case '9':
echo"\tNINE";
break;
default:
echo"wrong input!";
break;
}
$rev=(int)$rev/10;
}
}
?>
-----------------------------------------------------------------------------------------------------------------------------
2. Write a PHP script to change the background color of the browser using a switch
statement according to the day of the week.
ANS:
HTML AND PHP CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Change browser window color according to day of week!</h1>
<form action="Q2.php" method="post">
Enter A day of week: <select name="option">
<option value="1">Monday</option>
<option value="2">tuesday</option>
<option value="3">wednesday</option>
<option value="4">thursday</option>
<option value="5">friday</option>
<option value="6">saturday</option>
<option value="7">sunday</option>
</select>
<br>
<button>click</button>
</form>
</body>
</html>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$d=$_POST["option"];
switch ($d) {
case '1':
echo"<body style='background-color:red'>";
break;
case '2':
echo"<body style='background-color:blue'>";
break;
case '3':
echo"<body style='background-color:green'>";
break;
case '4':
echo"<body style='background-color:pink'>";
break;
case '5':
echo"<body style='background-color:orange'>";
break;
case '6':
echo"<body style='background-color:skyblue'>";
break;
case '7':
echo"<body style='background-color:purple'>";
break;
default:
echo"wrong input!";
break;
}
}
?>
-----------------------------------------------------------------------------------------------------------------------------
3. Write a PHP script to count the total number of even and odd numbers between
1 to 1000.
ANS:
HTML AND PHP CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>program to count even and odd numbers between given range</h1>
<form action="Q3.php" method="post">
<u>Enter start:</u><input type="text" name="start"><br>
<br>
<u>Enter End:</u><input type="text" name="End">
<br>
<button>click</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$s=$_POST["start"];
$e=$_POST["End"];
$o_count=0;
$e_count=0;
for ($i=$s; $i <= $e; $i++) {
if ($i%2==0) {
$e_count++;
}
else {
$o_count++;
}
}
echo"total even numbers between $s to $e are <strong>$e_count</strong>"."<br>";
echo"total odd numbers between $s to $e are <strong>$o_count</strong>"."<br>";
}
?>
-----------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment
Enter comment here!