Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Assignment Endsem

end

Uploaded by

2022pgcsca042
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Assignment Endsem

end

Uploaded by

2022pgcsca042
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Que 1. Write a PHP program to print alphabet pattern 'P'.

Expected Output:

****
* *
* *
****
*
*
*

<?php

$height = 7;
$width = 5;

for ($row = 0; $row < $height; $row++) {


for ($col = 0; $col < $width; $col++) {
if ($col == 0 || ($row == 0 || $row == $height / 2) && $col < $width - 1 || $col == $width - 1
&& ($row > 0 && $row < $height / 2)) {
echo "*";
} else {
echo " ";
}
}
echo "\n";
}

?>
<?php

$rows = 10;
$cols = 10;

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


for ($j = 1; $j <= $cols; $j++) {
echo ($i * $j) . " ";
}
echo "\n";
}

?>

Q.3:- Write a PHP script to remove nonnumeric characters except comma and dot.
Sample string : '$123,34.00A'
Expected Output : 12,334.00

<?php
$string = '$123,34.00A';
$cleanedString = preg_replace('/[^0-9,.]/', '', $string);
echo $cleanedString; // Output: 123,34.00
?>
Q.4:- Write a PHP program to check which number nearest to the value 100
among two given integers. Return 0 if the two numbers are equal.

Sample Input: 78, 95 95, 95 99, 70


Sample Output: 95 0 99

<?php
function nearestTo100($num1, $num2) {
if ($num1 == $num2) {
return 0;
}

$diff1 = abs(100 - $num1);


$diff2 = abs(100 - $num2);

if ($diff1 < $diff2) {


return $num1;
} elseif ($diff2 < $diff1) {
return $num2;
} else {
// If both differences are equal, return the larger number
return max($num1, $num2);
}
}

// Sample inputs
$inputs = [
[78, 95],
[95, 95],
[99, 70]
];

foreach ($inputs as $input) {


echo nearestTo100($input[0], $input[1]) . "\n";
}
?>
Q.5. Write a PHP program to create a new array from a given array of integers
shifting all zeros to left direction.

Sample Input: { 1, 2, 0, 3, 5, 7, 0, 9, 11 }
Sample Output: New array: 0,0,1,3,5,7,2,9,11

<?php
function shiftZerosToLeft($arr) {
$zeros = array_filter($arr, function($value) {
return $value === 0;
});

$nonZeros = array_filter($arr, function($value) {


return $value !== 0;
});

return array_merge($zeros, $nonZeros);


}

$input = [1, 2, 0, 3, 5, 7, 0, 9, 11];


$output = shiftZerosToLeft($input);

echo "New array: " . implode(",", $output) . "\n";


?>
Q6. Write a PHP class called 'Vehicle' with properties like 'brand', 'model', and
'year'. Implement a method to display the vehicle details.

<?php
class Vehicle {
public $brand;
public $model;
public $year;

public function __construct($brand, $model, $year) {


$this->brand = $brand;
$this->model = $model;
$this->year = $year;
}

public function displayDetails() {


echo "Brand: " . $this->brand . "\n";
echo "Model: " . $this->model . "\n";
echo "Year: " . $this->year . "\n";
}
}

// Create a new Vehicle object


$vehicle = new Vehicle("Toyota", "Corolla", 2022);

// Display the vehicle details


$vehicle->displayDetails();
?>
Q.7:- Write a PHP class called 'Student' with properties like 'name', 'age', and
'grade'. Implement a method to display student information.

<?php
class Student {
public $name;
public $age;
public $grade;

public function __construct($name, $age, $grade) {


$this->name = $name;
$this->age = $age;
$this->grade = $grade;
}

public function displayInfo() {


echo "Name: " . $this->name . "\n";
echo "Age: " . $this->age . "\n";
echo "Grade: " . $this->grade . "\n";
}
}

// Create a new Student object


$student = new Student("John Doe", 18, "A");

// Display the student information


$student->displayInfo();
?>

Q.8:- An array contains a set of 5 words. Write Javascript to reverse each word
and arrange the resulting words in alphabetical order. Make use of array
functions.

let words = ["hello", "world", "javascript", "programming", "algorithm"];

let reversedWords = words.map(word => word.split('').reverse().join(''));


reversedWords.sort();

console.log(reversedWords);
Q.9:- Selection for an army camp for boys is based on the following criteria.

i) Age group – 18 to 25
ii) Height - >=160cms
iii) Weight – 60Kgs SC/St students are given a relaxation of 5cms height & 5kgs
weight.

Accept the necessary details through form, do necessary validations and prepare
selection list using PHP.

Index.html

<!DOCTYPE html>
<html>
<head>
<title>Army Camp Selection</title>
</head>
<body>
<h2>Army Camp Selection</h2>
<form action="selection.php" method="post">
Age: <input type="number" name="age" required><br><br>
Height (in cms): <input type="number" name="height" required><br><br>
Weight (in kgs): <input type="number" name="weight" required><br><br>
Category:
<select name="category" required>
<option value="">Select Category</option>
<option value="General">General</option>
<option value="SC/ST">SC/ST</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Selection.php

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$age = $_POST["age"];
$height = $_POST["height"];
$weight = $_POST["weight"];
$category = $_POST["category"];

// Validate age, height, and weight


if ($age >= 18 && $age <= 25 && $height >= 160 && $weight >= 60) {
if ($category == "SC/ST") {
$height -= 5;
$weight -= 5;
}

echo "<h2>Selected Candidates:</h2>";


echo "Age: $age<br>";
echo "Height: $height cms<br>";
echo "Weight: $weight kgs<br>";
echo "Category: $category";
} else {
echo "<h2>Sorry, you do not meet the criteria for selection.</h2>";
}
}
?>

Que. 10
<html>
<head>
<title>Registration Form</title>
<script>
function showPassword() {
var x = document.getElementById("password");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
</script>
</head>
<body>
<h2>Registration Form</h2>
<form method="post" action="process.php">
<fieldset>
<legend>Name and Address</legend>
<label for="ssn">SSN:</label>
<input type="text" id="ssn" name="ssn" required><br><br>
<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname" required><br><br>
<label for="street">Street and Number:</label>
<input type="text" id="street" name="street" required><br><br>
<label for="postal_code">Postal Code:</label>
<input type="text" id="postal_code" name="postal_code" required><br><br>
<label for="city">City:</label>
<input type="text" id="city" name="city" required><br><br>
</fieldset>
<br>
<fieldset>
<legend>Username and Password</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="checkbox" onclick="showPassword()"> Show Password<br><br>
</fieldset>
<br>
<input type="submit" value="Register">
</form>
</body>
</html>
Q.11:- Using Node.js create a web page to read two created files names from user
and combine contents of both in to third one with all contents in Uppercase.

Index.html

<!DOCTYPE html>
<html>
<head>
<title>Combine Files</title>
</head>
<body>
<h2>Combine Files</h2>
<form action="/combine" method="post">
File 1 Name: <input type="text" name="file1" required><br><br>
File 2 Name: <input type="text" name="file2" required><br><br>
<input type="submit" value="Combine">
</form>
</body>
</html>

Server.html

const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();


app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', (req, res) => {


res.sendFile(__dirname + '/index.html');
});

app.post('/combine', (req, res) => {


const file1 = req.body.file1;
const file2 = req.body.file2;

// Read contents of file1


fs.readFile(file1, 'utf8', (err, data1) => {
if (err) {
return console.error(err);
}
// Read contents of file2
fs.readFile(file2, 'utf8', (err, data2) => {
if (err) {
return console.error(err);
}

// Combine contents of both files and convert to uppercase


const combinedData = data1.toUpperCase() + data2.toUpperCase();

// Write combined contents to a new file


fs.writeFile('combined.txt', combinedData, (err) => {
if (err) {
return console.error(err);
}
console.log('Files combined successfully!');
res.send('Files combined successfully!');
});
});
});
});

const PORT = 3000;


app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

Q.12. Write a PHP script to calculate the difference between two dates. Sample
dates : 1988-12-12, 2024-05-06 .

<?php
$date1 = new DateTime('1988-12-12');
$date2 = new DateTime('2024-05-06');

$interval = $date1->diff($date2);

echo "Difference: " . $interval->format('%y years, %m months, %d days') . "\n";


?>
Q.13 Write a PHP script to remove nonnumeric characters except comma and dot.

Sample string : '$123,34.00A'


Expected Output : 12,334.00

<?php
$string = '$123,34.00A';
$cleanedString = preg_replace('/[^0-9,.]/', '', $string);
echo $cleanedString; // Output: 12,334.00
?>

You might also like