Practical Programs Formatted
Practical Programs Formatted
7 Write a program to enter numbers till the user wants. At the end it 3
should display the count of positive, negative and zeros entered.
(Using do-while loop)
<!DOCTYPE html>
<html>
<head>
<title>Basic HTML Tags</title>
</head>
<body>
<h1>R22CA1CA0135</h1>
<p>PRATIK.</p>
<a href="https://example.com">Visit Example</a>
<img src="https://via.placeholder.com/150" alt="Sample Image">
<ul>
<li>R22CA1CA0135</li>
<li>Item Two</li>
</ul>
</body>
</html>
2. Write a Program to check and print whether a given number is even or odd.
<?php
$number = 7;
if ($number % 2 == 0) {
echo "$number is Even";
} else {
echo "$number is Odd";
}
?>
1
3. Write a program to compute net amount from the given quantity
purchased and rate per quantity. Discount @10% is allowed if the
quantity purchased exceeds 100.
<?php
$qty = 120;
$rate = 50;
$total = $qty * $rate;
<?php
$a = 10; $b = 20; $c = 15;
$largest = ($a > $b) ? (($a > $c) ? $a : $c) : (($b > $c) ? $b : $c);
echo "Largest number is: $largest";
?>
<?php
$num = 1234;
$sum = 0;
2
$digit = $num % 10;
$sum += $digit;
$num = floor($num / 10);
}
<?php
$n = 10;
$a = 0;
$b = 1;
echo "$a $b ";
7. Write a program to enter numbers till the user wants. At the end it
should display the count of positive, negative and zeros entered. (Using
do-while loop)
<?php
$positive = $negative = $zeros = 0;
do {
$num = (int)readline("Enter a number (type 999 to stop): ");
3
if ($num > 0) $positive++;
elseif ($num < 0) $negative++;
else $zeros++;
} while (true);
<?php
function countWords($str) {
$str = strtolower($str);
$words = str_word_count($str, 1);
$counted = array_count_values($words);
foreach ($counted as $word => $count) {
echo "$word: $count<br>";
}
}
9. Create a form with one text field and submit buttons for string length,
string reverse and uppercase, lowercase, string replace. Display the
result accordingly.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$str = $_POST["inputStr"];
$action = $_POST["action"];
switch ($action) {
case "length":
echo "Length: " . strlen($str);
break;
case "reverse":
echo "Reversed: " . strrev($str);
break;
case "upper":
echo "Uppercase: " . strtoupper($str);
break;
case "lower":
echo "Lowercase: " . strtolower($str);
break;
case "replace":
echo "Replaced: " . str_replace("a", "@", $str);
break;
}
}
?>
<?php
$a = 20;
$b = 10;
$choice = "multiply";
switch ($choice) {
case "add":
5
echo "Addition: " . ($a + $b);
break;
case "subtract":
echo "Subtraction: " . ($a - $b);
break;
case "multiply":
echo "Multiplication: " . ($a * $b);
break;
case "divide":
if ($b != 0) {
echo "Division: " . ($a / $b);
} else {
echo "Division by zero not allowed.";
}
break;
default:
echo "Invalid Choice";
}
?>