IWD4340704
IWD4340704
Practical :- 2(A)
CODE:-
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operation = $_POST['operation'];
switch ($operation) {
case 'add':
$result = $num1 + $num2;
break;
case 'subtract':
$result = $num1 - $num2;
break;
case 'multiply':
$result = $num1 * $num2;
break;
case 'divide':
if ($num2 != 0) {
$result = $num1 / $num2;
} else {
$result = "Cannot divide by zero";
}
break;
default:
$result = "Invalid operation";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<form method="post" action="">
<input type="number" name="num1" required placeholder="Enter first
number">
<input type="number" name="num2" required placeholder="Enter second
number">
<select name="operation" required>
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
<input type="submit" value="Calculate">
</form>
<?php
if (isset($result)) {
echo "<h2>Result: $result</h2>";
}
?>
OUTPUT-:
IWD4340704
Practical :- 3(A)
Aim: - Write a script that reads the name of the car and
displays the name of the
company the car belongs to
as per the below table:
car company
Safari,nexon,tiago,tigor Tata
XUV700,XUV300,Bolero Mahindra
I20,verna,venue,creta Hyundai
Swift,alto,baleno,brezza Suzuki
CODE: -
<?php
// Define an associative array where car names are keys and companies are values
$carCompanies = [
'Safari' => 'Tata', 'nexon' => 'Tata', 'tiago' => 'Tata', 'tigor' => 'Tata',
'XUV700' => 'Mahindra', 'XUV300' => 'Mahindra', 'Bolero' => 'Mahindra',
'I20' => 'Hyundai', 'verna' => 'Hyundai', 'venue' => 'Hyundai', 'creta' =>
'Hyundai',
'Swift' => 'Suzuki', 'alto' => 'Suzuki', 'baleno' => 'Suzuki', 'brezza' => 'Suzuki'
];
// Check if the car name exists in the array and display the company name
if (array_key_exists($carName, $carCompanies)) {
$response = "The company of the car '{$carName}' is: " .
$carCompanies[$carName];
} else {
$response = "Car not found in the database.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Car Company Finder</title>
</head>
<body>
<h1>Find the Company of a Car</h1>
<h2>Result:</h2>
<p><?php echo $response; ?></p>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 3(B)
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Sequence</title>
</head>
<body>
<h1>Fibonacci Sequence Generator</h1>
<form method="post" action="">
<input type="number" name="terms" required placeholder="Enter number
of terms">
<input type="submit" value="Generate">
</form>
<?php
if (isset($fibonacci)) {
echo "<h2>Fibonacci Sequence:</h2>";
echo implode(", ", array_slice($fibonacci, 0, $terms));
}
?>
</body>
</html>
OUTPUT- :
IWD4340704
Practical :- 3(C)
CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
$table = [];
<!DOCTYPE html>
<html>
<head>
<title>Multiplication Table</title>
</head>
<body>
<h1>Multiplication Table Generator</h1>
<form method="post" action="">
<input type="number" name="number" required
placeholder="Enter a number">
<input type="submit" value="Generate Table">
</form>
<?php
if (isset($table)) {
echo "<h2>Multiplication Table for $number:</h2>";
echo "<ul>";
foreach ($table as $line) {
echo "<li>$line</li>";
}
echo "</ul>";
}
?>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 4(A)
CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputString = $_POST['inputString'];
$length = 0;
$wordCount = 0;
$inWord = false;
<!DOCTYPE html>
<html>
<head>
<title>String Analysis</title>
</head>
<body>
<h1>String Length and Word Count</h1>
<form method="post" action="">
<textarea name="inputString" required placeholder="Enter
a string"></textarea>
<input type="submit" value="Analyze">
</form>
<?php
if (isset($length) && isset($wordCount)) {
echo "<h2>Results:</h2>";
echo "Length of the string: $length<br>";
echo "Number of words: $wordCount";
}
?>
</body>
</html>
OUTPUT-:
CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputArray = explode(',', $_POST['inputArray']);
$length = count($inputArray);
<!DOCTYPE html>
<html>
<head>
<title>Array Sorter</title>
</head>
<body>
<h1>Sort an Indexed Array</h1>
<form method="post" action="">
<input type="text" name="inputArray" required
placeholder="Enter numbers separated by commas">
<input type="submit" value="Sort">
</form>
<?php
if (isset($inputArray)) {
echo "<h2>Sorted Array:</h2>";
echo implode(", ", $inputArray);
}
?>
</body>
</html>
OUTPUT-: