PHP Assignment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Course Title: PHP & MySQL

Class: CA187 Assignment Solutions

Name: Mohamed Abdi Ahmed ID: C118201

1. Answer
<?php

$a = 42;

$b = 20;

$c = $a + $b;

echo "Addtion Operation Result: $c <br/>";

$c = $a - $b;

echo "Substraction Operation Result: $c <br/>";

$c = $a * $b;

echo "Multiplication Operation Result: $c <br/>";

$c = $a / $b;

echo "Division Operation Result: $c <br/>";

$c = $a % $b;

echo "Modulus Operation Result: $c <br/>";

$c = $a++;

echo "Increment Operation Result: $c <br/>";

$c = $a--;

echo "Decrement Operation Result: $c <br/>";

?>
2. Answer

<?php
$a=20;
$b=10;
$c=1;
if($a>$b && $a>$c)
{
echo "Greater value is a=".$a;
}
else if($b>$a && $b>$c)
{
echo "Greater value is b=".$b;
}
else if($c>$a && $c>$b)
{
echo "Greater value is c=".$c;
}
else
{
echo"Dont Enter Equal Values";
}
?>
3. Answer

<?php

function result($N)

for ($num = 0; $num < $N; $num++)

if ($num % 3 == 0 or $num % 5 == 0)

echo $num, " ";

$N = 20;

result($N);

?>
4. Answer

<?php
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach ($numbers as $index=>$value) {
if ($value % 2 == 0)
echo "$value <br>";
}
?>
-----------------------------------------------------------------------------------
<?php
echo 'The even number upto 35 are : ';
for($i=7; $i<=35; $i++)
{
if($i%2 == 0)

{
echo '<br>'.$i;
}

?>
5. Answer
<?php
function result($N)
{
for ($num = 2; $num < $N; $num++)
{
// Short-circuit operator is used
if ($num % 2 == 0 && $num % 5 == 0)
echo $num, " ";
}
}
$N = 50;
result($N);

?>
6. Answer
<?php

function reverse_number($number)
{
$snum = (string) $number;

$revstr = strrev($snum);

$reverse = (int) $revstr;

return $reverse;
}

echo reverse_number(12345);

?>
7. Answer
<?php
$x = 42;
$y = 70;

if ($x > $y) {


$temp = $x;
$x = $y;
$y = $temp;
}
for($i = 1; $i < ($x+1); $i++) {
if ($x%$i == 0 and $y%$i == 0)
$hcf = $i;
}

echo "HCF of $x and $y is: $hcf";


?>
8. Answer
<?php
$x = 8;
$y = 12;

if ($x > $y) {


$temp = $x;
$x = $y;
$y = $temp;
}

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


if ($x%$i == 0 && $y%$i == 0)
$gcd = $i;
}

$lcm = ($x*$y)/$gcd;
echo "LCM of $x and $y is: $lcm";
?>
9. Answer

<table align="center" border='1' width="100%">


<?php
$num = 12;

for($i = 1; $i <= 12; $i++)


{
echo "<tr>";

for($j =1; $j <= $num; $j++)


{
$multiplication_table = ($i * $j);
echo "<td>$j x $i = $multiplication_table </td>";
}

echo "<tr/>";
}
?>
</table>
10. Answer

<?php
function checkPrime($num)
{
if ($num == 1)
return 0;
for ($i = 2; $i <= $num/2; $i++)
{
if ($num % $i == 0)
return 0;
}
return 1;
}

echo '<h2>Prime Numbers between 1 and 100</h2> ';


for($num = 1; $num <= 100; $num++) {
$flag = checkPrime($num);
if ($flag == 1) {
echo $num." ";
}
}
?>
11. Answer

<?php
$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>

You might also like