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

myphpfile

The document contains a series of PHP programming exercises aimed at undergraduate students, covering various topics such as arithmetic operations, area calculations, quadratic equations, and string manipulations. Each section includes PHP code snippets to implement the tasks, along with expected outputs. The exercises also cover concepts like matrix operations, prime number determination, and file handling.

Uploaded by

bhavikas392
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

myphpfile

The document contains a series of PHP programming exercises aimed at undergraduate students, covering various topics such as arithmetic operations, area calculations, quadratic equations, and string manipulations. Each section includes PHP code snippets to implement the tasks, along with expected outputs. The exercises also cover concepts like matrix operations, prime number determination, and file handling.

Uploaded by

bhavikas392
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

1. Take values from the user and compute sum, subtraction, multiplication,
division and exponent of value of the variables.

<?php
$a = (float)readline("Enter the first number: ");
$b = (float)readline("Enter the second number: ");

echo "Sum: " . ($a + $b) . PHP_EOL;


echo "Subtraction: " . ($a - $b) . PHP_EOL;
echo "Multiplication: " . ($a * $b) . PHP_EOL;
echo "Division: " . ($b != 0 ? $a / $b : "Division by zero error") . PHP_EOL;
echo "Exponent: " . ($a ** $b) . PHP_EOL;
?>
Output:-

Page | 1
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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


triangle, square, trapezoid and parallelogram.

<?php
function areaOfShapes() {
echo "1. Circle\n2. Rectangle\n3. Triangle\n4. Square\n5. Trapezoid\n6. Parallelogram\n";
$choice = readline("Enter the shape number to calculate its area: ");

switch ($choice) {
case 1:
$radius = (float)readline("Enter the radius: ");
echo "Area of Circle: " . (pi() * $radius * $radius) . PHP_EOL;
break;
case 2:
$length = (float)readline("Enter the length: ");
$width = (float)readline("Enter the width: ");
echo "Area of Rectangle: " . ($length * $width) . PHP_EOL;
break;
case 3:
$base = (float)readline("Enter the base: ");
$height = (float)readline("Enter the height: ");
echo "Area of Triangle: " . (0.5 * $base * $height) . PHP_EOL;
break;
case 4:
$side = (float)readline("Enter the side length: ");
echo "Area of Square: " . ($side * $side) . PHP_EOL;
break;
case 5:
$a = (float)readline("Enter the first base: ");
$b = (float)readline("Enter the second base: ");
$height = (float)readline("Enter the height: ");
echo "Area of Trapezoid: " . (0.5 * ($a + $b) * $height) . PHP_EOL;
break;
case 6:

Page | 2
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

$base = (float)readline("Enter the base: ");


$height = (float)readline("Enter the height: ");
echo "Area of Parallelogram: " . ($base * $height) . PHP_EOL;
break;
default:
echo "Invalid choice!" . PHP_EOL;
}
}
areaOfShapes();
?>
Output:-

Page | 3
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

3. Compute and print roots of quadratic equation.

<?php
$a = (float)readline("Enter the coefficient a: ");
$b = (float)readline("Enter the coefficient b: ");
$c = (float)readline("Enter the coefficient c: ");

$discriminant = $b ** 2 - 4 * $a * $c;
if ($discriminant < 0) {
echo "No real roots, discriminant is negative." . PHP_EOL;
} elseif ($discriminant == 0) {
$root = -$b / (2 * $a);
echo "Single root: $root" . PHP_EOL;
} else {
$root1 = (-$b + sqrt($discriminant)) / (2 * $a);
$root2 = (-$b - sqrt($discriminant)) / (2 * $a);
echo "Roots are $root1 and $root2" . PHP_EOL;
}
?>
Output:-

Page | 4
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

4. Write a program to determine whether a triangle is isosceles or not?

<?php
$a = (float)readline("Enter side a: ");
$b = (float)readline("Enter side b: ");
$c = (float)readline("Enter side c: ");

if ($a == $b || $b == $c || $a == $c) {
echo "The triangle is isosceles." . PHP_EOL;
} else {
echo "The triangle is not isosceles." . PHP_EOL;
}
?>
Output:-

Page | 5
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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

<?php
$num = (int)readline("Enter a number: ");
for ($i = 1; $i <= 10; $i++) {
echo "$num x $i = " . ($num * $i) . PHP_EOL;
}
?>
Output:-

Page | 6
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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

<?php
$n = (int)readline("Enter a number: ");
$sum = ($n * ($n + 1)) / 2;
echo "Sum of natural numbers up to $n is $sum" . PHP_EOL;
?>
Output:-

Page | 7
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

7. Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13 21…..n

<?php
$n = (int)readline("Enter the number of terms: ");
$f1 = 0;
$f2 = 1;

echo "$f1 $f2 ";


for ($i = 3; $i <= $n; $i++) {
$next = $f1 + $f2;
echo "$next ";
$f1 = $f2;
$f2 = $next;
}
echo PHP_EOL;
?>
Output:-

Page | 8
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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

<?php
$num = (int)readline("Enter a number: ");
$factorial = 1;

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


$factorial *= $i;
}
echo "Factorial of $num is $factorial" . PHP_EOL;
?>
Output:-

Page | 9
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

9. Determine prime numbers within a specific range.

<?php
$start = (int)readline("Enter the start of the range: ");
$end = (int)readline("Enter the end of the range: ");

echo "Prime numbers between $start and $end: ";


for ($i = $start; $i <= $end; $i++) {
$isPrime = true;
if ($i < 2) $isPrime = false;
for ($j = 2; $j <= sqrt($i); $j++) {
if ($i % $j == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) echo "$i ";
}
echo PHP_EOL;
?>
Output:-

Page | 10
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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

<?php
$marks = [];
$numSubjects = (int)readline("Enter the number of subjects: ");

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


$marks[] = (float)readline("Enter marks for subject $i: ");
}

$average = array_sum($marks) / count($marks);

echo "Average Marks: $average" . PHP_EOL;

if ($average >= 90) {


echo "Grade: A" . PHP_EOL;
} elseif ($average >= 80) {
echo "Grade: B" . PHP_EOL;
} elseif ($average >= 70) {
echo "Grade: C" . PHP_EOL;
} elseif ($average >= 60) {
echo "Grade: D" . PHP_EOL;
} else {
echo "Grade: F" . PHP_EOL;
}
?>
Output:-

Page | 11
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

11. Compute addition, subtraction and multiplication of a matrix.

<?php
echo "Enter the elements of the first matrix (2x2):\n";
$matrix1 = [
[(int)readline("Matrix1[0][0]: "), (int)readline("Matrix1[0][1]: ")],
[(int)readline("Matrix1[1][0]: "), (int)readline("Matrix1[1][1]: ")]
];

echo "Enter the elements of the second matrix (2x2):\n";


$matrix2 = [
[(int)readline("Matrix2[0][0]: "), (int)readline("Matrix2[0][1]: ")],
[(int)readline("Matrix2[1][0]: "), (int)readline("Matrix2[1][1]: ")]
];

// Addition
echo "Addition:\n";
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
echo $matrix1[$i][$j] + $matrix2[$i][$j] . " ";
}
echo "\n";
}
?>
Output:-

Page | 12
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

Page | 13
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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

<?php
$string = "Develop & Empower Individuals";
$vowelCount = preg_match_all('/[aeiouAEIOU]/', $string);
echo "Input: \"$string\"\n";
echo "Total vowels: $vowelCount\n";
?>
Output:-

Page | 14
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

13. Determine whether a string is palindrome or not?

<?php
$string = readline("Enter a string: ");
$reversed = strrev($string);
echo "Input: $string\n";
if ($string === $reversed) {
echo "The string is a palindrome.\n";
} else {
echo "The string is not a palindrome.\n";
}
?>
Output:-

Page | 15
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

14. Display word after Sorting in alphabetical order.

<?php
$string = readline("Enter a sentence: ");
$words = explode(" ", $string);
sort($words);
echo "Input: $string\n";
echo "Sorted: " . implode(" ", $words) . "\n";
?>
Output:-

Page | 16
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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

<?php
$num = (int)readline("Enter a number: ");
$start = (int)readline("Enter the start of the range: ");
$end = (int)readline("Enter the end of the range: ");
echo "Input: $num, Range: [$start, $end]\n";
if ($num >= $start && $num <= $end) {
echo "The number is in the range.\n";
} else {
echo "The number is not in the range.\n";
}
?>
Output:-

Page | 17
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

16. Write a program accepts a string and calculates number of upper case letters and lower
case letters available in that string.

<?php
$string = readline("Enter a string: ");
$uppercase = preg_match_all('/[A-Z]/', $string);
$lowercase = preg_match_all('/[a-z]/', $string);
echo "Input: $string\n";
echo "Uppercase: $uppercase\n";
echo "Lowercase: $lowercase\n";
?>
Output:-

Page | 18
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

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

<?php
$string = readline("Enter a sentence: ");
$words = explode(" ", $string);
$reversed = implode(" ", array_reverse($words));
echo "Input: $string\n";
echo "Reversed: $reversed\n";
?>
Output:-

Page | 19
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

18. Write a program to create a login form. On submitting the form, the user should navigate
to profile page.

<!-- login.html -->


<!DOCTYPE html>
<html>
<body>
<form action="profile.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

<!-- profile.php -->


<?php
$username = $_POST['username'];
echo "Welcome, $username!\n";
?>
Output:-

Page | 20
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

19. Design front page of a college or department using graphics method.

<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; background-color: #f0f0f0; text-align: center; }
.header { background-color: #4CAF50; color: white; padding: 10px; }
.content { margin: 20px; }
</style>
</head>
<body>
<div class="header">
<h1>Welcome to XYZ College</h1>
</div>
<div class="content">
<p>Empowering the Future</p>
</div>
</body>
</html>
Output:-

Page | 21
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005

20. Write a program to upload and download files.

<!-- upload.html -->


<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload: <input type="file" name="fileToUpload">
<input type="submit" value="Upload File">
</form>
</body>
</html>

<!-- upload.php -->


<?php
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "uploads/" .
$_FILES["fileToUpload"]["name"])) {
echo "File uploaded successfully!";
} else {
echo "Error uploading file.";
}
?>
Output:-

Page | 22

You might also like