0% found this document useful (0 votes)
11 views3 pages

Answer Sheet

The document contains multiple PHP scripts for various tasks including checking divisibility by 5 or 10, printing a star pattern, checking if a number is prime, determining voting eligibility based on age, swapping two numbers, and performing basic arithmetic operations. Each task is presented with HTML forms for user input and corresponding PHP logic to process the input. The scripts demonstrate fundamental programming concepts such as loops, conditionals, and form handling in PHP.

Uploaded by

bejpapai
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)
11 views3 pages

Answer Sheet

The document contains multiple PHP scripts for various tasks including checking divisibility by 5 or 10, printing a star pattern, checking if a number is prime, determining voting eligibility based on age, swapping two numbers, and performing basic arithmetic operations. Each task is presented with HTML forms for user input and corresponding PHP logic to process the input. The scripts demonstrate fundamental programming concepts such as loops, conditionals, and form handling in PHP.

Uploaded by

bejpapai
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
You are on page 1/ 3

ANSWER SHEET

1. Enter the range and print all the numbers which is divisible by 5 or 10 [User Input ].
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Divisibility Check in Range</title>
</head>
<body>
<center>
<h3>Check Divisibility by 5 or 10 in a Range</h3>
<form action="" method="get">
<input type="number" name="start" placeholder="Start of Range"><br><br>
<input type="number" name="end" placeholder="End of Range"><br><br>
<button type="submit">Submit</button>
</form>

<?php
$start =$_GET['start'];
$end =$_GET['end'];

for ($i = $start; $i <= $end; $i++) {


if ($i % 10 == 0 || $i % 5 == 0) {
echo $i;
}
}

?>
</center>
</body>
</html>
2. Write a program in php and print the pattern using nested.
<?php
$rows = 5; // Number of rows
for ($i = $rows; $i >= 1; $i--) {
for ($j = 1; $j <= $i; $j++) {
echo "*"; // Print star
}
echo "<br>"; // Move to the next line after each row
}
?>
3. Write a program to check whether a given number is prime or not [user input].
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Number Checker</title>
</head>
<body>
<h1>Prime Number Checker</h1>
<form method="post">
<label for="number">Enter a number:</label>
<input type="text" id="number" name="number" required>
<button type="submit">Check Prime</button>
</form>

<?php
if (isset($_POST['number'])) {
$number = $_POST['number'];

$i = 2;
while ($i <= $number / 2) {
if ($number % $i == 0) {
echo "<h2>$number is not a prime number.</h2>";
return; // Exit immediately if a divisor is found
}
$i++;
}
echo "<h2>$number is a prime number.</h2>";

}
?>
</body>
</html>
4. Write a PHP script to check if a person is eligible to vote (age >= 18).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head><center>
<body>
<form method="GET">
Enter your Age: <input type="number" name="num" >
<button type="submit" >Calculate The Age Of A Voter</mark> </button>
</form>

<?php
$age=$_GET['num'];
if($age>=18){
echo "$age is a eligible for vote";
}else{
echo "$age is not eligible for vote";
}
?>
</center>
</body>
</html>
5. Write a PHP program to swap two numbers using a third variable.
<?php
// Define two variable
$num1 = 10;
$num2 = 20;

// Display numbers before swapping


echo "Before swapping: num1 = $num1, num2 = $num2\n";

// Using a third variable to swap


$temp = $num1;
$num1 = $num2;
$num2 = $temp;

// Display numbers after swapping


echo "After swapping: num1 = $num1, num2 = $num2\n";
?>
6. Write a PHP script to perform addition, subtraction, multiplication, and division of two numbers
<?php
// Define two numbers
$num1 = 10;
$num2 = 20;

// Perform addition, subtraction, multiplication, and division of two number


$sum = $num1 + $num2;
$difference = $num1 - $num2;
$multiply = $num1 * $num2;
$division= $num1 / $num2;

// Display results
echo "Addition: $num1 + $num2 = $sum\n";
echo "Subtraction: $num1 - $num2 = $difference\n";
echo "Multiplication: $num1 * $num2 = $ multiply \n";
echo "Division: $num1 / $num2 = $division\n";
?>

You might also like