0% found this document useful (0 votes)
5 views20 pages

PHP File

The document contains a series of PHP programs that perform various mathematical and logical operations, such as basic arithmetic, area calculations for different shapes, quadratic equation roots, and prime number determination. It also includes programs for string manipulation, matrix operations, and user interactions like login forms and file uploads. Each program is followed by its output example, showcasing its functionality.

Uploaded by

hxrshvisuals
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)
5 views20 pages

PHP File

The document contains a series of PHP programs that perform various mathematical and logical operations, such as basic arithmetic, area calculations for different shapes, quadratic equation roots, and prime number determination. It also includes programs for string manipulation, matrix operations, and user interactions like login forms and file uploads. Each program is followed by its output example, showcasing its functionality.

Uploaded by

hxrshvisuals
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

UGCA-1930 2332891

Program 1
1. Take values from the user and compute sum, subtraction, multiplication,
division and exponent of value of the variables.
<?php
$a = readline("Enter first number: ");
$b = readline("Enter second number: ");

echo "Sum = " . ($a + $b) . "\n";


echo "Subtraction = " . ($a - $b) . "\n";
echo "Multiplication = " . ($a * $b) . "\n";
echo "Division = " . ($a / $b) . "\n";
echo "Exponent = " . ($a ** $b) . "\n";
?>

Output

1
UGCA-1930 2332891

Program 2
2. Write a program to find area of following shapes: circle, rectangle,
triangle, square, trapezoid and parallelogram.
<?php
define("PI", 3.1416);
$radius = readline("Enter radius of circle: ");
echo "Area of Circle: " . (PI * $radius * $radius) . "\n";
$length = readline("Enter length of rectangle: ");
$width = readline("Enter width of rectangle: ");
echo "Area of Rectangle: " . ($length * $width) . "\n";
$base = readline("Enter base of triangle: ");
$height = readline("Enter height of triangle: ");
echo "Area of Triangle: " . (0.5 * $base * $height) . "\n";
$side = readline("Enter side of square: ");
echo "Area of Square: " . ($side * $side) . "\n";
$a = readline("Enter base1 of trapezoid: ");
$b = readline("Enter base2 of trapezoid: ");
$h = readline("Enter height of trapezoid: ");
echo "Area of Trapezoid: " . (0.5 * ($a + $b) * $h) . "\n";
$base = readline("Enter base of parallelogram: ");
$height = readline("Enter height of parallelogram: ");
echo "Area of Parallelogram: " . ($base * $height) . "\n";
?>

Output

2
UGCA-1930 2332891

Program 3
3. Compute and print roots of quadratic equation.
<?php
$a = readline("Enter a: ");
$b = readline("Enter b: ");
$c = readline("Enter c: ");
$d = ($b*$b) - (4*$a*$c);
if ($d > 0) {
$root1 = (-$b + sqrt($d)) / (2*$a);
$root2 = (-$b - sqrt($d)) / (2*$a);
echo "Roots are real: $root1 and $root2\n";
} elseif ($d == 0) {
$root = -$b / (2*$a);
echo "Roots are equal: $root\n";
} else {
echo "No real roots\n";
}
?>

Output

3
UGCA-1930 2332891

Program 4
4. Write a program to determine whether a triangle is isosceles or not?
<?php
$a = readline("Enter side a: ");
$b = readline("Enter side b: ");
$c = readline("Enter side c: ");
if ($a == $b || $b == $c || $a == $c)
echo "It is an Isosceles Triangle\n";
else
echo "Not an Isosceles Triangle\n";
?>

Output

4
UGCA-1930 2332891

Program 5
5. Print multiplication table of a number input by the user.
<?php
$num = readline("Enter number: ");
for ($i = 1; $i <= 10; $i++) {
echo "$num x $i = " . ($num * $i) . "\n";
}
?>

Output

5
UGCA-1930 2332891

Program 6
6. Calculate sum of natural numbers from one to n number.
<?php
$n = readline("Enter n: ");
$sum = ($n * ($n + 1)) / 2;
echo "Sum of first $n natural numbers = $sum\n";
?>

Output

6
UGCA-1930 2332891

Program 7
7. Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13 21…..n
<?php
$n = readline("Enter number of terms: ");
$a = 0; $b = 1;
echo "$a $b ";
for ($i = 2; $i < $n; $i++) {
$c = $a + $b;
echo "$c ";
$a = $b;
$b = $c;
}
echo "\n";
?>

Output

7
UGCA-1930 2332891

Program 8
8. Write a program to find the factorial of any number.
<?php
$num = readline("Enter a number: ");
$fact = 1;
for ($i = 1; $i <= $num; $i++) {
$fact *= $i;
}
echo "Factorial of $num is $fact\n";
?>

Output

8
UGCA-1930 2332891

Program 9
9. Determine prime numbers within a specific range.
<?php
$start = readline("Enter start number: ");
$end = readline("Enter end number: ");
echo "Prime numbers between $start and $end:\n";
for ($i = $start; $i <= $end; $i++) {
if ($i < 2) continue;
$isPrime = true;
for ($j = 2; $j <= sqrt($i); $j++) {
if ($i % $j == 0) {
$isPrime = false;
break;}}
if ($isPrime) echo "$i ";}
echo "\n";
?>

Output

9
UGCA-1930 2332891

Program 10
10. Write a program to compute, the Average and Grade of students
marks.
<?php
$marks = [85, 90, 78, 88, 92]; // example marks
$total = array_sum($marks);
$avg = $total / count($marks);
echo "Average Marks: $avg\n";
if ($avg >= 90)
echo "Grade: A+";
elseif ($avg >= 80)
echo "Grade: A";
elseif ($avg >= 70)
echo "Grade: B";
elseif ($avg >= 60)
echo "Grade: C";
else
echo "Grade: Fail";
?>

Output

10
UGCA-1930 2332891

Program 11
11. Compute addition, subtraction and multiplication of a matrix.
<?php
$a = [[1,2],[3,4]];
$b = [[5,6],[7,8]];
echo "Addition:\n";
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++)
echo ($a[$i][$j] + $b[$i][$j]) . " ";
echo "\n";
}
echo "\nSubtraction:\n";
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++)
echo ($a[$i][$j] - $b[$i][$j]) . " ";
echo "\n";
}
echo "\nMultiplication:\n";
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++) {
$sum = 0;
for ($k=0; $k<2; $k++)
$sum += $a[$i][$k] * $b[$k][$j];
echo "$sum ";
}
echo "\n";}
?>

Output

11
UGCA-1930 2332891

Program 12
12. Count total number of vowels in a word “Develop & Empower
Individuals”.
<?php
$str = "Develop & Empower Individuals";
$vowels = ['a','e','i','o','u','A','E','I','O','U'];
$count = 0;
for ($i = 0; $i < strlen($str); $i++) {
if (in_array($str[$i], $vowels))
$count++;
}
echo "Total vowels: $count\n";
?>

Output

12
UGCA-1930 2332891

Program 13
13. Determine whether a string is palindrome or not?
<?php
$str = "madam";
if ($str == strrev($str))
echo "$str is a Palindrome\n";
else
echo "$str is NOT a Palindrome\n";
?>

Output

13
UGCA-1930 2332891

Program 14
14. Display word after Sorting in alphabetical order.
<?php
$str = "hello world welcome to php";
$words = explode(" ", $str);
sort($words);
echo "Sorted Words: " . implode(" ", $words) . "\n";
?>

Output

14
UGCA-1930 2332891

Program 15
15. Check whether a number is in a given range using functions.
<?php
function inRange($num, $min, $max) {
return ($num >= $min && $num <= $max);
}
$num = 25; $min = 10; $max = 50;
if (inRange($num, $min, $max))
echo "$num is in range\n";
else
echo "$num is NOT in range\n";
?>

Output

15
UGCA-1930 2332891

Program 16
16. Write a program accepts a string and calculates number of upper case
letters and lower case letters available in that string.
<?php
$str = "Hello PHP World";
$upper = 0;
$lower = 0;
for ($i = 0; $i < strlen($str); $i++) {
if (ctype_upper($str[$i])) $upper++;
elseif (ctype_lower($str[$i])) $lower++;
}
echo "Uppercase: $upper\n";
echo "Lowercase: $lower\n";
?>

Output

16
UGCA-1930 2332891

Program 17
17. Design a program to reverse a string word by word.
<?php
$str = "I love programming";
$words = explode(" ", $str);
$reversed = array_reverse($words);
echo implode(" ", $reversed) . "\n";
?>

Output

17
UGCA-1930 2332891

Program 18
18. Write a program to create a login form. On submitting the form, the
user should navigate to profile page.
<?php
$username = readline("Enter username: ");
$password = readline("Enter password: ");
if ($username == "admin" && $password == "1234")
echo "Login successful! Welcome to your profile page.\n";
else
echo "Invalid login.\n";
?>

Output

18
UGCA-1930 2332891

Program 19
19. Design front page of a college or department using graphics method.
<?php
echo "*****************************\n";
echo " ABC COLLEGE OF ENGINEERING\n";
echo "*****************************\n";
echo "Courses Offered:\n";
echo "1. [Link]\n2. [Link]\n3. MBA\n4. MCA\n";
echo "Contact: info@[Link]\n";
?>

Output

19
UGCA-1930 2332891

Program 20
20. Write a program to upload and download files.
<?php
// Create a new file (simulate upload)
$file = '[Link]';
file_put_contents($file, "This is uploaded content.");
// Read file (simulate download)
$content = file_get_contents($file);
echo "Downloaded Content:\n$content\n";
?>

Output

20

You might also like