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

PHP Assignment

The document contains an individual assignment submitted by Adem Ahmed Bekar with ID 0239/13. It includes 10 programming questions to be solved in PHP. The questions cover topics like simple calculator using switch case, for loops, nested loops, recursion, functions for calculating factorials, Fibonacci series, greatest common divisor and checking for palindromes. Detailed code solutions and explanations are provided for each question.

Uploaded by

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

PHP Assignment

The document contains an individual assignment submitted by Adem Ahmed Bekar with ID 0239/13. It includes 10 programming questions to be solved in PHP. The questions cover topics like simple calculator using switch case, for loops, nested loops, recursion, functions for calculating factorials, Fibonacci series, greatest common divisor and checking for palindromes. Detailed code solutions and explanations are provided for each question.

Uploaded by

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

COLLEGE OF COMPUTING AND STATICS

DEPARTMENT OF SOFTWARE ENGINEERING


PHP INDIVIDUAL ASSIGNMENT
INDIVIDUAL ASSIGNMENT
NAME IDNO
ADEM AHMED BEKAR…………….0239/13

SUBMITTTED TO: DR ABDALGAYN


1,Write a simple calculator program in PHP using switch case.
Operations:
Addition
Subtraction
Multiplication
Division
****************************HTML PART*************
<!DOCTYPE html>
<html lang="en">

<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>

<form method="post" action="num1calculator.php">


<label for="num1">Num 1:</label>
<input class="class1" type="number" name="num1" placeholder="Enter num"
required><br><br>

<label for="num2">Num 2:</label>


<input class="class2" type="number" name="num2" placeholder="Enter num"
required><br><br>

<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>

<input class="btn" type="submit" value="Calculate">


</form>
</body>

</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'];

// Perform calculation based on the selected operator


switch ($operator) {
case 'add':
$result = $num1 + $num2;
$operatorSymbol = '+';
break;
case 'subtract':
$result = $num1 - $num2;
$operatorSymbol = '-';
break;
case 'multiply':
$result = $num1 * $num2;
$operatorSymbol = '*';
break;
case 'divide':
if ($num2 != 0) {
$result = $num1 / $num2;
$operatorSymbol = '/';
} else {
echo "Error: Division by zero is not allowed.";
}
break;
default:
echo "Error: Invalid operator selected.";
break;
}

// Display the result


echo "Result: {$num1} {$operatorSymbol} {$num2} = {$result}";
}
?>
</body>

</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);
}
}

echo "The first 10 Fibonacci numbers are: ";


for ($i = 0; $i < 10; $i++) {
echo fibonacci($i) . " ";
}
?>
8,Write a function named gcd that accepts two integers as parameters and returns the
greatest
common divisor (GCD) of the two numbers. The greatest common divisor of two integers a
and b is the largest integer that is a factor of both a and b. The GCD of any number and 1 is
1,
and the GCD of any number and 0 is that number.
One efficient way to compute the GCD is to use Euclid's algorithm, which states the
following:
gcd(a, b) = gcd(b, a % b)
gcd(a, 0) = Absolute value of a
For example:
gcd(24, 84) returns 12,
gcd(105, 45) returns 15, and
gcd(0, 8) returns 8
<?php
function gcd($a, $b)
{
if ($b == 0) {
return abs($a);
} else {
return gcd($b, $a % $b);
}
}
$result = gcd(100, 50);
echo "The greatest common divisor is = $result";
?>
9. Vowel Count: Write a function called vowelCount which accepts a string as a parameter,
and
returns the number of vowels in the in string. Vowels include a, e, i, o, and u. You may
assume
the string will be all lowercase.
<!DOCTYPE html>
<html lang="en">

<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");

echo "result 1 is =$result";


echo "<br/>";
echo "result 2 is = $result2";
echo "<br/>";
echo "result 1 is =$result3";
echo "<br/>";

echo "result 4 is = $result4";


echo "<br/>";

echo "result 5 is =$result5";


?>

You might also like