0% found this document useful (0 votes)
0 views

Assignments 00

The document contains a series of PHP programming assignments that cover basic arithmetic operations, conditional statements, variable manipulation, and calculations related to geometry and marks. Each assignment includes a brief description followed by a PHP script that implements the required functionality. The exercises aim to enhance understanding and application of PHP programming concepts.

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)
0 views

Assignments 00

The document contains a series of PHP programming assignments that cover basic arithmetic operations, conditional statements, variable manipulation, and calculations related to geometry and marks. Each assignment includes a brief description followed by a PHP script that implements the required functionality. The exercises aim to enhance understanding and application of PHP programming concepts.

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/ 9

Assignment 1:Basics in PHP

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;

if ($x >= $y && $x >= $z)


$max = $x;
if ($y >= $x && $y >= $z)
$max = $y;
if ($z >= $x && $z >= $y)
$max = $z;

echo "Largest number among $x, $y and $z is: $max\n";


}

largest(100, 50, 25);


largest(50, 50, 25);
?>

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;

// Computes the quotient


$qu = $dn / $dr;

// Computes the remainder


$rem = $dn % $dr;

echo "The Quotient is " . $qu . "\n";


echo "The Remainder is " . $rem;

?>

2. Write a PHP Script to swap the values of two variables.

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);
}

function surfaceAreaCuboid($l, $h, $w)


{
return (2 * $l * $w + 2 * $w *
$h + 2 * $l * $h);
}

// Driver Code
$l = 1;
$h = 5;
$w = 7;
echo "Area = " ,
areaCuboid($l, $h, $w) , "\n";
echo "Total Surface Area = ",
surfaceAreaCuboid($l, $h, $w);

// This code is contributed by ajit


?>
2. Write a PHP Script to calculate the area of Circle, Square, and Rectangle.

Solution:

<html>

<body>

<form method = "post">

Width: <input type="number" name="width">

<br><br>

Length: <input type="number" name="length"> <br>

<input type = "submit" name = "submit" value="Calculate">

</form>

</body>

</html>

<?php

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

$width = $_POST['width'];

$length = $_POST['length'];

$area = $width*$length;

echo "The area of a rectangle with $width x $length is $area";

?>

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>

You might also like