0% found this document useful (0 votes)
7 views20 pages

IWD4340704

The document contains a series of PHP scripts for various practical programming exercises, including creating a simple calculator, identifying car companies based on names, generating Fibonacci sequences, displaying multiplication tables, and analyzing strings for length and word count. Each practical exercise includes the PHP code and HTML forms for user interaction. The document demonstrates fundamental programming concepts and web development techniques.

Uploaded by

vrajc2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views20 pages

IWD4340704

The document contains a series of PHP scripts for various practical programming exercises, including creating a simple calculator, identifying car companies based on names, generating Fibonacci sequences, displaying multiplication tables, and analyzing strings for length and word count. Each practical exercise includes the PHP code and HTML forms for user interaction. The document demonstrates fundamental programming concepts and web development techniques.

Uploaded by

vrajc2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

IWD4340704

Practical :- 2(A)

Aim :- write a script to implement a simple calculator for


mathematical operations

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'
];

// Initialize the response message


$response = "";
// Check if the form is submitted and if the car name is provided
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['carName'])) {
$carName = trim($_POST['carName']);

// Convert car name to lowercase to handle case-insensitive matching


$carName = strtolower($carName);

// 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>

<!-- HTML Form to input the car name -->


<form method="POST" action="">
<label for="carName">Enter the name of the car:</label><br>
<input type="text" id="carName" name="carName" required><br><br>
<button type="submit">Find Company</button>
</form>

<h2>Result:</h2>
<p><?php echo $response; ?></p>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 3(B)

Aim: - Write a script to display Fibonacci numbers up to a


given term.
CODE- :
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$terms = $_POST['terms'];
$fibonacci = [0, 1];

for ($i = 2; $i < $terms; $i++) {


$fibonacci[$i] = $fibonacci[$i - 1] + $fibonacci[$i - 2];
}
}
?>

<!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)

Aim: - Write a script to display a multiplication table for


the given number.

CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
$table = [];

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


$table[] = "$number x $i = " . ($number * $i);
}
}
?>

<!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)

Aim: - Write a script to calculate the length of a string and


count the number of words in the given string without
using string functions.

CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputString = $_POST['inputString'];
$length = 0;
$wordCount = 0;
$inWord = false;

// Calculate length and count words


for ($i = 0; isset($inputString[$i]); $i++) {
$length++;
if ($inputString[$i] != ' ') {
if (!$inWord) {
$wordCount++;
$inWord = true;
}
} else {
$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-:

Write a script to sort a given indexed array.


IWD4340704
Practical :- 4(B)

Aim: - Write a script to sort a given indexed array.

CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputArray = explode(',', $_POST['inputArray']);
$length = count($inputArray);

// Simple bubble sort algorithm


for ($i = 0; $i < $length - 1; $i++) {
for ($j = 0; $j < $length - $i - 1; $j++) {
if ($inputArray[$j] > $inputArray[$j + 1]) {
// Swap
$temp = $inputArray[$j];
$inputArray[$j] = $inputArray[$j + 1];
$inputArray[$j + 1] = $temp;
}
}
}
}
?>

<!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-:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy