php pracital file (1)
php pracital file (1)
Take values from the user and compute sum, subtraction, multiplication, division and exponent
of value of the variables
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post">
</form>
<?php
if (isset($_POST['submit'])) {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
echo "<h2>Results:</h2>";
?>
</body>
</html>
Q2.Write a program to find area of following shapes: circle, rectangle, triangle, square, trapezoid and
parallelogram.
<!DOCTYPE html>
<html lang="en">
<title>Document</title>
</head><body>
<?php
//area of circle
$radius=8;
$area=3.14*$radius*$radius;
//area of rectangle
$length=7;
$breadth=8;
$area= $length*$breadth;
//area of triangle
$base=3;$height=4;
$area=1/2*$base*$height;
//area of square
$sides=9;
$area=$sides*$sides;
//area of parallelogram
$a=10;$b=11;
$area=$a*$b;
//area of trapezium
$c=5;$d=6;
$area=1/2*($c+$d)*10;
echo "<br>";
?>
</body>
</html>
Q3.Compute and print roots of quadratic equation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$discriminant = $b * $b - 4 * $a * $c;
if ($discriminant > 0) {
} elseif ($discriminant == 0) {
} else {
// Complex roots
?>
</body>
</html>
Q4.Write a program to determine whether a triangle is isosceles or not?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$side1 = $_POST['side1'];
$side2 = $_POST['side2'];
$side3 = $_POST['side3'];
} else {
?>
</body>
</html>
Q5.Print multiplication table of a number input by the user.
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num = $_POST['num'];
$limit = $_POST['limit'];
?>
Q6.Calculate sum of natural numbers from one to n number.
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n = $_POST['n'];
$sumLoop = 0;
$sumLoop += $i;
?>
Q7.Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13 21…..n.
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n = $_POST['n'];
$num1 = 0;
$num2 = 1;
$num1 = $num2;
$num2 = $num3;
echo "<br>";
function fibonacci($n) {
if ($n == 0) {
return 0;
} elseif ($n == 1) {
return 1;
} else {
echo "<br>";
?>
Q8.Write a program to find the factorial of any number.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Factorial Calculator</title>
</head>
<body>
<h1>Factorial Calculator</h1>
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
function factorial($n) {
if ($n == 0 || $n == 1) {
return 1;
} else {
$result = factorial($number);
?>
</body>
</html>
Q9.Determine prime numbers within a specific range.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$start = $_POST['start'];
$end = $_POST['end'];
echo "<h2>Please enter a valid range (both numbers must be >= 2 and start <= end).</h2>";
} else {
$primeNumbers = [];
if (isPrime($num)) {
$primeNumbers[] = $num;
}
if (empty($primeNumbers)) {
} else {
function isPrime($num) {
return false;
return true;
?>
</body>
</html>
Q10.Write a program to compute, the Average and Grade of students marks.
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$sub1 = $_POST['sub1'];
$sub2 = $_POST['sub2'];
$sub3 = $_POST['sub3'];
$grade = 'A';
$grade = 'B';
$grade = 'C';
$grade = 'D';
} else {
$grade = 'F';
?>
Q11.Compute addition, subtraction and multiplication of a matrix.
<form method="post">
</label><br>
</label><br>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$rowsA = $_POST['rowsA'];
$colsA = $_POST['colsA'];
$rowsB = $_POST['rowsB'];
$colsB = $_POST['colsB'];
// Reshape matrices
// Display matrices
printMatrix($matrixA);
printMatrix($matrixB);
// Matrix addition
printMatrix(addMatrix($matrixA, $matrixB));
} else {
// Matrix subtraction
printMatrix(subtractMatrix($matrixA, $matrixB));
} else {
// Matrix multiplication
if ($colsA == $rowsB) {
printMatrix(multiplyMatrix($matrixA, $matrixB));
} else {
function printMatrix($matrix) {
$result = array();
for ($i = 0; $i < count($matrixA); $i++) {
$row = array();
$result[] = $row;
return $result;
$result = array();
$row = array();
$result[] = $row;
return $result;
$result = array();
$row = array();
$sum = 0;
$row[] = $sum;
$result[] = $row;
}
return $result;
?>
Q12.Count total number of vowels in a word “Develop & Empower Individuals”.
<?php
function countVowels($string) {
$string = strtolower($string);
return count($matches[0]);
$totalVowels = countVowels($word);
echo "<br>";
?>
Q13.Determine whether a string is palindrome or not?
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$str = $_POST['str'];
if (strrev($str) == $str) {
} else {
echo "<br>";
$isPalindrome = true;
$len = strlen($str);
$isPalindrome = false;
break;
if ($isPalindrome) {
} else {
echo "<br>";
return true;
}
if ($str[$start] != $str[$end]) {
return false;
} else {
?>
Q14.Display word after Sorting in alphabetical order.
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$words = $_POST['words'];
$words = trim(strtolower($words));
$wordArray = array_filter($wordArray);
sort($wordArray);
echo "<br>";
natcasesort($wordArray);
echo "<br>";
?>
Q15.Check whether a number is in a given range using functions.
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num = $_POST['num'];
$min = $_POST['min'];
$max = $_POST['max'];
return true;
} else {
return false;
} else {
echo "<br>";
return ($num >= $min && $num <= $max) ? true : false;
} else {
echo "$num is not within the range [$min, $max].";
echo "<br>";
} else {
?>
Q16.Write a program accepts a string and calculates number of upper case letters and lower case
letters available in that string.
<form method="post">
</form>
<?php
echo "<br>";
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$str = $_POST['str'];
$upper = 0;
$lower = 0;
$char = $str[$i];
if (ctype_upper($char)) {
$upper++;
} elseif (ctype_lower($char)) {
$lower++;
$counts = array_count_values($chars);
?>
Q17.Design a program to reverse a string word by word.
<!DOCTYPE html>
<html>
<head>
<title>String Reversal</title>
</head>
<body>
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$str = $_POST['str'];
?>
</body>
</html>
Q18.Write a program to create a login form. On submitting the form, the user should navigate to
profile page.
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h2>Login Form</h2>
<form method="post">
<button type="submit">Login</button>
</form>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $username;
header("Location: profile.php");
exit;
} else {
?>
</body>
</html>
Q19.Design front page of a college or department using graphics method
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
</style>
</head>
<body>
<header>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Courses</a>
</nav>
</header>
<div class="container">
<h2>Welcome!</h2>
</div>
<footer>
<p>© 2024 Department of Computer Science</p>
</footer>
</body>
</html>
Q20.Write a program to upload and download files.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
</style>
</head>
<body>
<h2>Upload File</h2>
</form>
<?php
if (isset($_POST['upload'])) {
$uploadOk = 1;
if (file_exists($targetFile)) {
$uploadOk = 0;
echo "Sorry, only JPG, PNG, GIF, PDF & TXT files are allowed.<br>";
$uploadOk = 0;
if ($uploadOk == 0) {
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
} else {
if (is_dir($targetDir)) {
if ($dh = opendir($targetDir)) {
closedir($dh);
} else {
}
?>
</body>
</html>