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

Oaish Qazi 1|Page

Practical No 3:

Q. Write a Program to print whether the number is Armstrong or not using while loop.
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
//Write a Program to print whether the number is Armstrong or not using while loop.
$num = 153;
$sum = 0;
$temp = $num;

while($num > 0){


$rem = $num % 10;
$sum += $rem * $rem * $rem;
$num = $num / 10;
}

if($sum == $temp){
echo "Number is Armstrong";
}
else{
echo "Number is not Armstrong";
}
?>
</head>
</html>
OUTPUT:

Q. Write a Program to print whether the number is the Palindrome or not using while loop.
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
//Write a Program to print whether the number is the Palindrome or not using while loop.
$num = 121;
$temp = $num;
Oaish Qazi 2|Page

$rev = 0;
while ($num > 0) {
$rem = $num % 10;
$rev = $rev * 10 + $rem;
$num = floor($num / 10);
}

if ($temp == $rev) {
echo "The number is Palindrome";
} else {
echo "The number is not Palindrome";
}
?>
</head>
</html>

OUTPUT:

Q. Write a Program to print whether the number is Prime or not using for loop.
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
//Write a Program to print whether the number is Prime or not using for loop.
$n = 71;
$isPrime = true;
for ($i = 2; $i < $n; $i++) {
if ($n % $i == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) {
echo $n." is Prime";
} else {
echo $n." is not Prime";
}
?>
</head>
</html>

OUTPUT:
Oaish Qazi 3|Page

Q. Write a Program to print whether the year is the Leap Year or not using if else statement
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
//Write a Program to print the Right angle triangle using for while loop.
$n = 5;
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo "*";
}
echo "<br>";
}
?>
</head>
</html>

OUTPUT:

Q. Write a Program to print the Equilateral triangle using for loop.


CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
//Write a Program to print the Equilateral triangle using for loop.

for($i=1;$i<=5;$i++){
Oaish Qazi 4|Page

for($j=5;$j>$i;$j--){
echo "&nbsp;";
}
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br>";
}

?>
</head>
</html>

OUTPUT:

Q. Write a Program to print the Fibonacci Series.


CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
//Write a Program to print the Fibonacci Series.
$a = 0;
$b = 1;
$c = 0;
echo "Fibonacci Series: ".$a . " " . $b . " ";
for ($i = 0; $i < 10; $i++) {
$c = $a + $b;
echo $c . " ";
$a = $b;
$b = $c;
}
?>
</head>
</html>

OUTPUT:
Oaish Qazi 5|Page

Q. Write a Program to create a Menu Driven Program using do while loop.


CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
// Write a Program to create a Menu Driven Calculator Program using do while loop.
$a = 10;
$b = 20;
$num = 1;
$choice = 0;

do {
echo "1. Addition<br>";
echo "2. Subtraction<br>";
echo "3. Multiplication<br>";
echo "4. Division<br>";
echo "5. Exit<br>";
$choice = $num;
echo "Enter your choice: ".$choice."<br>";
$num++;
switch ($choice) {
case 1:
$result = $a + $b;
echo "Addition of $a and $b is $result<br>";
break;
case 2:
$result = $a - $b;
echo "Subtraction of $a and $b is $result<br>";
break;
case 3:
$result = $a * $b;
echo "Multiplication of $a and $b is $result<br>";
break;
case 4:
$result = $a / $b;
echo "Division of $a and $b is $result<br>";
break;
case 5:
exit;
default:
echo "Invalid choice<br>";
}
Oaish Qazi 6|Page

} while ($choice != 5);


?>
</head>
</html>

OUTPUT:

You might also like