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

PHP Practical File

The document contains a PHP practical file submitted by a student. It provides 20 solutions to programming problems involving basic PHP concepts like taking user input, performing calculations, working with strings, arrays, loops, functions and files. The problems cover topics such as calculating area of shapes, roots of quadratic equations, prime numbers, factorials, matrices, checking palindromes, sorting words and more. Each solution is well commented and documented with sample input/output to demonstrate the code working as expected.

Uploaded by

Tushar Thamman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
755 views

PHP Practical File

The document contains a PHP practical file submitted by a student. It provides 20 solutions to programming problems involving basic PHP concepts like taking user input, performing calculations, working with strings, arrays, loops, functions and files. The problems cover topics such as calculating area of shapes, roots of quadratic equations, prime numbers, factorials, matrices, checking palindromes, sorting words and more. Each solution is well commented and documented with sample input/output to demonstrate the code working as expected.

Uploaded by

Tushar Thamman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PHP PRACTICAL FILE

SUBMITTED BY:-Tushar Thamman


ROLL NO:-1929410
E-MAIL:[email protected]
Index

1. Take values from the user and compute sum,


subtraction, multiplication, division and exponent of
value of the variables.
2. Write a program to find area of following shapes:
circle, rectangle, triangle, square, trapezoid and
parallelogram.
3. Compute and print roots of quadratic equation.
4. Write a program to determine whether a triangle
is isosceles or not?
5. Print multiplication table of a number input by
the user.
6. Calculate sum of natural numbers from one to n
number.
7. Print Fibonacci series up to n numbers e.g. 0 1 1 2
3 5 8 13 21.....n
8. Write a program to find the factorial of any
number.
9. Determine prime numbers within a specific range.
10. Write a program to compute, the Average and
Grade of students marks.
11. Compute addition, subtraction and
multiplication of a matrix.
12. Count total number of vowels in a word
“Develop & Empower Individuals”.
13. Determine whether a string is palindrome or
not?
14. Display word after Sorting in alphabetical order.
15. Check whether a number is in a given range
using functions.
16. Write a program accepts a string and calculates
number of upper case letters
and lower case letters available in that string.
17. Design a program to reverse a string word by
word.
18. Write a program to create a login form. On
submitting the form, the user
should navigate to profile page.
19. Design front page of a college or department
using graphics method.
20. Write a program to upload and download files.
Solutions :

1. Take values from the user and compute sum, subtraction,


multiplication, division and exponent of value of the
variables.
<?php
$x=100;
$y=60;
echo "The sum of x and y is : ". ($x+$y) ."<br />";
echo "The difference between x and y is : ". ($x-$y) ."<br />";
echo "Multiplication of x and y : ". ($x*$y) ."<br />";
echo "Division of x and y : ". ($x/$y) ."<br />";
echo "Modulus of x and y : " . ($x%$y) ."<br />";
?>

2. Write a program to find area of following shapes: circle,


rectangle, triangle, square, trapezoid and parallelogram.
<?php
/*Same Size Sides*/
function square($var) {
return $var * $var;
}
function equilateral_triangle($side) {
return 0.43301 * ($side * $side);
}
/*Two Different Size Sides*/
function rectangle($vala, $valb) {
return $vala * $valb;
}
function parallelogram($base, $height) {
return $base * $height;
}
function triangle($base, $height) {
return 0.5 * $base * $height;
}
/*One Radius*/
function circle($radius) {
return 3.14 * ($radius * $radius);
}
/*Two Different Radius*/
function ellipse($radius1, $radius2) {
return 3.14 * $radius1 * $radius2;
}
/*Two Different Base*/
function trapezoid($height, $base1, $base2) {
return (($base1 + $base2) / 2) * $height;
}

echo"The Area of square is : ".square(4)."<br />"; // 16


echo "The Area of equilateral triangle is : ".equilateral_triangle(4)."<br />"; //
6.92816
echo"The Area of circle is : ".circle(15)."<br />"; // 706.5
echo"The Area of rectangle is : ".rectangle(15,15)."<br />"; // 225
echo"The Area of parallelogram is : ".parallelogram(15,5)."<br />"; // 75
echo"The Area of triangle is : ".triangle(15,5)."<br />"; // 37.5
echo"The Area of ellipse is : ".ellipse(15,5)."<br />"; // 235.5
echo"The Area of trapezoid is : ".trapezoid(10,10,10)."<br />"; // 100
?>
3. Compute and print roots of quadratic equation.
<?php
function roots($a, $b, $c) {
$D = $b*$b - 4*$a*$c;
if ($D >= 0){
$x1 = (-$b + sqrt($D))/(2*$a);
$x2 = (-$b - sqrt($D))/(2*$a);
echo "Roots are: $x1, $x2 \n";
} else {
$x1 = -$b/(2*$a);
$x2 = sqrt(-$D)/(2*$a);
echo "Roots are: $x1 ± $x2 i \n";
}
}

echo "Equation is x*x+5x+4=0\n"."<br/>";


roots(1,5,4);
echo"<br/>";
echo "\nEquation is x*x+4x+5=0\n"."<br/>";
roots(1,4,5);
?>

4. Write a program to determine whether a triangle is


isosceles or not?
<html>
<head>
<title>PHP Program To read three sides of a triangle and print the type of the
triangle</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter length of
1st side"/>
</td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter length of
2nd side"/>
</td>
</tr>
<tr>
<td> <input type="text" name="num3" value="" placeholder="Enter length of
3rd side"/>
</td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$a = $_POST['num1'];
$b = $_POST['num2'];
$c = $_POST['num3'];
echo "Given sides : ".$a." ".$b." ".$c. "</br>";
if(($a==$b) || ($b==$c) || ($c==$a))
{
echo("Isosceles triangle");
}
else
{
echo("not isosceles triangle");
}
return 0;
}
?>
</body>
</html>

5. Print multiplication table of a number input by the user.


<html>
<head>
<title>practical</title>
</head>
<body >
<form method="post" action="table.php">
<strong>Enter No:</strong>
<input type="text" name="num" size="10">
<input type="submit" value="Get Table">
</form>
</body>
</html>

<?php
error_reporting(0);
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>

6. Calculate sum of natural numbers from one to n number.


<?php
$n = 10;
$sum = 0;

//calculating sum from 1 to n


for($i = 1; $i <= $n; $i++)
$sum += $i;

echo "Sum is: $sum";

?>
7. Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8
13 21…..n
<?php
$num = 0;
$n1 = 0;
$n2 = 1;
echo "<h3>Fibonacci series for first 12 numbers: </h3>";
echo "\n";
echo $n1.' '.$n2.' ';
while ($num < 10 )
{
$n3 = $n2 + $n1;
echo $n3.' ';
$n1 = $n2;
$n2 = $n3;
$num = $num + 1;
}
?>

8. Write a program to find the factorial of any number.


<?php
$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>

9. Determine prime numbers within a specific range.


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

10. Write a program to compute, the Average and Grade of


students marks.
<?php
$p = 95;
$c = 85;
$m = 74;
$e = 64;
$h = 53;
$total = NULL;
$average = NULL;
$percentage = NULL;

// p, c, m, e, and h are the five subjects


// p = physics
// c = chemistry
// m = math
// e = english
// h = history

// Calculate total, average and percentage


$total = $p + $c + $m + $e + $h;
$average = $total / 5.0;
$percentage = ($total / 500.0) * 100;

// Output
echo "The Total marks = " . $total . "/500\n";
echo "<br>";
echo "The Average marks = " . $average . "\n";
echo "<br>";
echo "The Percentage = " . $percentage . "%";
echo "<br>";
?>

11. Compute addition, subtraction and multiplication of a


matrix.
<?php
$arr1 = array(
array(1, 1, 1),
array(1, 1, 1),
array(1, 1, 1),
);
$arr2 = array(
array(2, 2, 2),
array(2, 2, 2),
array(2, 2, 2),
);

echo "Matrix A (3 x 3): <br>";


for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
echo $arr1[$i][$j] . " ";
echo "<br>";
}

echo "<br>Matrix B (3 x 3): <br>";


for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
echo $arr2[$i][$j] . " ";
echo "<br>";
}

echo "<br>Matrix Additon (A + B): <br>";


for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
{
$arr3[$i][$j] = $arr1[$i][$j] + $arr2[$i][$j];
echo $arr3[$i][$j] . " ";
}
echo "<br>";
}

echo "<br>Matrix Subtraction (A - B): <br>";


for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
{
$arr3[$i][$j] = $arr1[$i][$j] - $arr2[$i][$j];
echo $arr3[$i][$j] . " ";
}
echo "<br>";
}

echo "<br>Matrix Multiplication (A * B): <br>";


for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
{
$arr3[$i][$j] = $arr1[$i][$j] * $arr2[$i][$j];
echo $arr3[$i][$j] . " ";
}
echo "<br>";
}
?>

12. Count total number of vowels in a word “Develop &


Empower Individuals”.
<?php
$count=0;
$a="Develop and Empower Individuals";
for($i=0;$i<strlen($a);$i=$i+1)
{<
if($a{$i}=='a'||$a{$i}=='e'||$a{$i}=='i'||$a{$i}=='o'||$a{$i}=='u') //here $a{i}
will return the character for ith position
{
$count=$count+1;
}
}
echo " String is :$a"."<br/>";
echo "The number of vowels are:-".$count;
?>

13.Check whether the string is palindrome or not


<?php
// example to get the palindrome of a string using built in function
//input string is MADAM
$input = "MADAM";
echo '<br> Input String '. $input;
//reverse of input string - MADAM - using strrev
$reverse = strrev($input);
echo '<br> Ouput String '. $reverse;
//condition to check if the input and the reverse of the string is equal or not
if($input == $reverse) {
echo '<br> '.$input.' is a palindrome';
}
else {
echo '<br>'.$input.' is not a palindrome';
}
?>

14. Display word after Sorting in alphabetical order.


<?php

$fruits=array("lemon","orange","banana","apple");
sort($fruits);
foreach($fruits as $key=>$val){
echo $val."<br/>";
}

15. Check whether a number is in a given range using


functions.
<?php
$value=5;
$min=0;
$max=10;
if($min<$value && $max>$value){
echo "Number is in range";
}
else{
echo "Number is not in range";
}
?>
16. Write a program accepts a string and calculates number
of upper case letters and lower case letters available in that
string.
<?php
function Countt($str)
{
$upper = 0;
$lower = 0;
$number = 0;
$special = 0;
for ($i = 0; $i < strlen($str); $i++)
{
if ($str[$i] >= 'A' &&
$str[$i] <= 'Z')
$upper++;
else if ($str[$i] >= 'a' &&
$str[$i] <= 'z')
$lower++;
else if ($str[$i]>= '0' &&
$str[$i]<= '9')
$number++;
else
$special++;
}
echo "Upper case letters: " , $upper,"\n" ."<br/>";
echo "Lower case letters : " ,$lower,"\n"."<br/>" ;
echo "Number : " , $number ,"\n"."<br/>";
echo "Special characters : ", $special ;
}

$str = "#BCA123Jyoti@XYZ*YADAV";
Countt($str);

?>

17. Design a program to reverse a string word by word.


<?php
$string = "PCTE GROUP OF INSTITUTES";
echo "Reverse string of $string is " .strrev ( $string );
?>

You might also like