Assignments 00
Assignments 00
Practice Programs:
1. Write a PHP script to perform arithmetic operations on two numbers
(Addition, Subtraction,Multiplication Division ).
Solution:
<?php
/* Variable definition and assignment */
$a=10;
$b=20;
/* Arithmetic operations */
/*Addition*/
echo "The Sum of the two numbers is ". ($a+$b) . ".<br>";
/*Subtraction*/
echo "The Difference of two numbers is ". ($a-$b) .".<br>";
/*Multiplication*/
echo "The Product of the two numbers is ". ($a*$b) .".<br>";
/*Division*/
echo "The Quotient of the two numbers is ". ($a/$b) .".<br>";
/*Modulus*/
echo "The Remainder of the two numbers is " . ($a%$b) .".<br>";
?>
2. Write a PHP script to display a maximum of two numbers using a conditional
operator.
Solution:
<?php
function largest($x, $y, $z) {
$max = $x;
3. Write a PHP script that will perform pre and post-increment of a number.
(Example ++a,a++).
<?php
$a = 10;
echo 'Value of $a is :'.$a;
echo '<br />After Pre-increment value of $a ( i.e. ++$a ) is: '.++$a;
$a = 20;
echo '<br />Value of $a is :'.$a;
echo '<br />After Post-increment value of $a ( i.e. $a++ ) is: '.$a++;
$a = 30;
echo '<br />Value of $a is :'.$a;
echo '<br />After Pre-decrement value of $a ( i.e. --$a ) is: '.--$a;
$a = 40;
echo '<br />Value of $a is :'.$a;
echo '<br />After Post-decrement value of $a ( i.e. $a-- ) is: '.$a--;
?>
SET A
1. Write a PHP Script to display Quotient and Remainder of the division of two variables.
Solution
<?php
// Write a program to find
// quotient and remainder in PHP language
$dn = 44;
$dr = 25;
?>
Solution:
1. <?php
2. $a = 45;
3. $b = 78;
4. // Swapping Logic
5. $third = $a;
6. $a = $b;
7. $b = $third;
8. echo "After swapping:<br><br>";
9. echo "a =".$a." b=".$b;
10. ?>
3. Write a PHP Script which will convert temperatures from Celsius(C)to Fahrenheit (F).
(Hint:C=5.0/9(F-32)
Solution:
1. <?php
2. $celsius = 17 ;
3. $fahrenheit = (($celsius*9)/5)+32 ;
4. echo("Temperature in Fahrenheit is: ");
5. echo($fahrenheit);
6. ?>
SET B
1. Write a PHP Script to display the surface area and volume of a cuboid.
(Hint: surface area=2(lb+lh+bh ), volume = l*b*h )
Solution:
<?php
// PHP program to find volume and
// total surface area of cuboid
// utility function
function areaCuboid($l, $h, $w)
{
return ($l * $h * $w);
}
// Driver Code
$l = 1;
$h = 5;
$w = 7;
echo "Area = " ,
areaCuboid($l, $h, $w) , "\n";
echo "Total Surface Area = ",
surfaceAreaCuboid($l, $h, $w);
Solution:
<html>
<body>
<br><br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
$width = $_POST['width'];
$length = $_POST['length'];
$area = $width*$length;
?>
3. Write a PHP Script to display the total and percentage of Marks of Subjects (Out of 100)Data
Structure, Digital Marketing, PHP, SE, and Bigdata.
Solution:
//To read the marks of three subjects and display the Total, Avg and Class
<html>
<head>
<title>PHP Program To read the marks of three subjects and display the
Total, Avg and Class</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter
English marks"/>
</td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter Maths
marks"/>
</td>
</tr>
<tr>
<td> <input type="text" name="num3" value="" placeholder="Enter
Science marks"/>
</td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$english = $_POST['num1'];
$maths = $_POST['num2'];
$science = $_POST['num3'];
$precision = 4;
$total = ($english + $maths + $science); // Compute total marks
$average = number_format($total/3, $precision); //compute the average of
total marks
echo " Total marks of three subjects = ".$total ."";
echo " Average marks of three subjects = ".$average ."";
if($total<35 || $english<35 || $maths<35 || $science<35)
{
echo "Fail";
}
else
if($average>=60 && $average<70)
{
echo "Third Class";
}
else
if($average>=70 && $average<80)
{
echo "Second Class";
}
else
if($average>=80 && $average<95)
{
echo "First Class";
}
else
{
echo "Distinction";
}
return 0;
}
?>
</body>
</html>