PHP Assignment
PHP Assignment
<head>
<title>Simple Calculator</title>
<link rel="stylesheet" href="body.css" />
</head>
<body>
<p> Department of Software Engineering<br>php assingnment<br> Adem Ahmed
Bekar <br>IDNO 0239/13 </p>
<h2>Simple Calculator</h2>
<label for="operator">Operator:</label>
<select name="operator">
<option value="add">Addition (+)</option>
<option value="subtract">Subtraction (-)</option>
<option value="multiply">Multiplication (*)</option>
<option value="divide">Division (/)</option>
</select><br><br>
</html>
*******************PHP PART*************************************
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
</head>
<body>
<h1>Calculated Result</h1>
<?php
// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve values from the form
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];
</html>
2,Create a PHP script using a for loop to add all the integers between 0 and 30 and display the
total.
***********answer*************
<?php $sum = 0;
for ($i = 0; $i <= 30; $i++) {
$sum += $i;
}
?>
echo "The sum of the integers between 0 and 30 is $sum";
3,Create a script to construct the following pattern, using a nested for loop.
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
##############answer###############
<?php
$n = 5;
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j <= $i; $j++) {
echo '* ';
}
echo "<br/>";
}
for ($i = $n; $i > 0; $i--) {
for ($j = 0; $j < $i; $j++) {
echo '* ';
}
echo "<br/>";
}
?>
4,Write a program to calculate and print the factorial of a number using a for loop. The factorial
of a number is the product of all integers up to and including that number, so the factorial of
4 is 4*3*2*1= 24
<?php function factorial($n)
{
$result = 1;
for ($i = 1; $i <= $n; $i++) {
$result *= $i;
}
return $result;
}
$number = 10;
$factorial = factorial($number);
echo "The factorial of $number is $factorial";
?>
5,Write a program in PHP to print prime numbers between 1 and 100.
<?php $primes = [];
for ($i = 2; $i <= 100; $i++) {
$isPrime = true;
for ($j = 2; $j * $j <= $i; $j++) {
if ($i % $j == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) {
$primes[] = $i;
}
}
echo "The prime numbers between 1 and 100 are: ";
foreach ($primes as $prime) {
echo $prime . ", ";
}
?>
6. Write a program to print numbers from 10 to 1 using recursion function.
<?php
function printNumbers($n)
{
if ($n > 1) {
echo $n . " ";
printNumbers($n - 1);
} else {
echo $n;
}
}
printNumbers(10);
?>
7,Write a PHP function to print fibonacci series.
<?php
function fibonacci($n)
{
if ($n < 2) {
return $n;
} else {
return fibonacci($n - 1) + fibonacci($n - 2);
}
}
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
function count_Vowels($string)
{
//checks if the letters are found in the words
preg_match_all('/[aeiou]/i', $string, $matches);
return count($matches[0]);
}
print("The vowel in AdemAhmed is ");
print_r(count_Vowels('AdemAhmed'));
?>
</body>
</html>
10. Palindromes: Write a function isPalindrome that accepts a string as a parameter and
returns
true if the string is a palindrome and false otherwise. A string is considered a palindrome if it
has the same sequence of letters when reversed (for example, "radar", "toot", "mom", "a",
""). Your function should be case-insensitive; for example, "Mom" and "RAdar" should be
considered palindromes.
<?php
function isPalindrome($string)
{
$reverse = strrev($string);
return $string === $reverse;
}
$result = isPalindrome("abccba");
$result2 = isPalindrome("vav");
$result3 = isPalindrome("gaaGAA");
$result4 = isPalindrome("YOOOOY");
$result5 = isPalindrome("woow");