0% found this document useful (0 votes)
35 views30 pages

Endterm Final Print

The document contains 5 programming problems and their solutions in JavaScript and PHP. It proposes algorithms to: 1. Change text color based on a dropdown selection. 2. Sort an array input by the user. 3. Validate form fields on submission. 4. Generate a welcome message with the user's name on button click. 5. Check if a string is a "snowball string". It then provides a PHP script to display all prime numbers from 1 to 100. For each problem, pseudocode is given before providing the full code solution.
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)
35 views30 pages

Endterm Final Print

The document contains 5 programming problems and their solutions in JavaScript and PHP. It proposes algorithms to: 1. Change text color based on a dropdown selection. 2. Sort an array input by the user. 3. Validate form fields on submission. 4. Generate a welcome message with the user's name on button click. 5. Check if a string is a "snowball string". It then provides a PHP script to display all prime numbers from 1 to 100. For each problem, pseudocode is given before providing the full code solution.
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/ 30

Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem Statement :- 1 Write a javascript code to change the color of text according to user choice from the drop
down list with color names.

Algorithm:-
1. Create a dropdown list with color options.
2. Add an element (e.g., a paragraph) whose text color will be changed.
3. Create a function (changeTextColor) that is triggered when the user selects a color from the
dropdown.
4. Get the selected color from the dropdown.
5. Change the text color of the element using the selected color.
Code and output
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Color Changer</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
</style>
</head>
<body>
<br>
<br>
<h1>Select a Color</h1>
<h2 id="akt">Anukalp Kumar thakur MCA 1st C-21 (1102699)</h2>

<label for="colorSelector">Choose a color:</label>


<select id="colorSelector" onchange="changeTextColor()">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

<option value="orange">Orange</option>
<option value="purple">Purple</option>
<option value="black">Black</option>
<option value="yellow">yellow</option>
</select>

<p id="textToChange">This is some text.</p>

<script>
function changeTextColor() {
// Get the selected color from the dropdown
var selectedColor = document.getElementById("colorSelector").value;

// Change the text color based on the user's selection


document.getElementById("textToChange").style.color = selectedColor;
document.getElementById("akt").style.color = selectedColor;
}
</script>

</body>
</html>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem Statement:-2 Write a javascript code to sort an array taken as input from the user
Algorithm
1. Start

2. Create an empty array to store user input.

3. Prompt the user to enter the length of the array and store it in a variable (let's call it 'length').

4. Loop for 'length' times:


a. Prompt the user to enter an element and push it into the array.

5. Display the original array to the user.

6. Use a sorting algorithm (e.g., bubble sort, quicksort, mergesort) to sort the array.

7. Display the sorted array to the user.

8. End
Code and output
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Sorter</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
#m
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

{color: green;}
#a
{color: purple;
}
}
</style>
</head>
<body>
<br>
<br>
<h1>Array Sorter by </h1>
<h2 id="a">Anukalp Kumar Thakur<h2>
<p id="m">Enter numbers separated by commas:</p>
<input type="text" id="inputArray" placeholder="e.g., 5, 3, 8, 1">

<button onclick="sortArray()">Sort</button>

<p id="sortedArray"></p>

<script>
function sortArray() {
// Get the input array from the user
var inputArrayString = document.getElementById("inputArray").value;

// Convert the input string into an array of numbers


var inputArray = inputArrayString.split(',').map(function(item) {
return parseInt(item, 10);
});

// Sort the array


Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

inputArray.sort(function(a, b) {
return a - b; // For numeric sorting
});

// Display the sorted array


document.getElementById("sortedArray").textContent = "Sorted Array: " + inputArray;
}
</script>

</body>
</html>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem Statement:-3 :-Write a javascript code to validate registration form input fields.
1. Start

2. Create a function (e.g., validateForm) to perform form validation.

3. Inside the function:


a. Get the values of input fields (e.g., username, email, password) from the form.

b. Perform validations for each field:


- Check if the username is not blank.
- Check if the email is in a valid format (contains '@' and '.').
- Check if the password meets certain criteria (e.g., minimum length).

c. Display appropriate error messages if any validation fails.

d. If all validations pass, submit the form or perform further actions.

4. Attach the validateForm function to the form's onsubmit event.

5. End
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem Statement:- 4- Write a javascript code to create a text content with welcome message in red color on
button click. Take username as input.

Algorithm:
1. Start

2. Create a function (e.g., generateWelcomeMessage) to be triggered on the button click.

3. Inside the function:


a. Get the username from the input field.

b. Check if the username is provided. If not, display an alert and return.

c. Create a welcome message using the username.

d. Display the welcome message in a specific element (e.g., a paragraph) in red color.

4. Attach the generateWelcomeMessage function to the button's onclick event.

6. End
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem statement:- 5 - Write a javascript code to find whether a given string is ‘Snowball String’ or not. A
snowball string is one, whose length of each word, are in ascending order and consecutive too. for example- "I am
the king" length of I=1 am =2 the=3 king

Algorithm:-
1. Start

2. Create a function (e.g., isSnowballString) that takes a string as input.

3. Inside the function:


a. Split the string into an array of words.

b. Loop through each word in the array:


i. Get the length of the current word.

ii. Check if the length is greater than the length of the previous word (if not the first word).
- If not, return false.

c. If the loop completes without returning false, return true.

4. End
Code and output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snowball String Checker</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

margin: 50px;
}
</style>
</head>
<body>

<h1>Snowball String Checker</h1>

<label for="inputString">Enter a string:</label>


<input type="text" id="inputString" placeholder="Type your string here">

<button onclick="checkSnowballString()">Check</button>

<p id="result"></p>

<script>
function checkSnowballString() {
// Get the input string
var inputString = document.getElementById("inputString").value.trim();

// Split the string into words


var words = inputString.split(/\s+/);

// Check if the length of each word is in ascending order and consecutive


var isSnowballString = true;
for (var i = 1; i < words.length; i++) {
if (words[i].length !== words[i - 1].length + 1) {
isSnowballString = false;
break;
}
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

// Display the result


var resultMessage = isSnowballString ? "It's a Snowball String!" : "It's not a Snowball String.";
document.getElementById("result").textContent = resultMessage;
}
</script>

</body>
</html>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

PHP
Problem statement :- 6 - Write a PHP script to display the prime numbers from 1-100.
Algorithm
1. Start local host for running php code, I am using WAMP server for this having port no 3306

2. Create a function (e.g., isPrime) to check if a number is prime.


a. If the number is less than 2, return false.
b. Loop from 2 to the square root of the number:
i. If the number is divisible evenly by any value in the loop, return false.
c. If the loop completes without finding a divisor, return true.

3. Loop from 1 to 100:


a. Check if the current number is prime using the isPrime function.
b. If prime, display the number.

4. End
Code and output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Numbers</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
</style>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

</head>
<body>

<h1>Prime Numbers from 1 to 100</h1>

<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}

for ($i = 2; $i <= sqrt($number); $i++) {


if ($number % $i == 0) {
return false;
}
}

return true;
}

echo "<p>";
for ($i = 1; $i <= 100; $i++) {
if (isPrime($i)) {
echo $i . " ";
}
}
echo "</p>";
?>

</body>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

</html>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem Statement:-7 Write a PHP script to search a given digit in a number


Algorithm

1. Start
2. Create a function (e.g., searchDigit) that takes two parameters - the number and the digit to search. a.
Convert the number to a string for easier digit manipulation. b. Loop through each digit in the string:
i. If the current digit matches the search digit, return true. c. If the loop completes without finding the
digit, return false.3. Take input from the user for the number and digit.
4. Call the searchDigit function with the provided number and digit.5. Display whether the digit is found
or not.
6. End
Code and Output:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Digit Search</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
</style>
</head>
<body>

<h1>Digit Search</h1>

<form method="post">
<label for="number"> Enter a number :</label>
<input type="number" name="number" required>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

<br>

<label for="digit">Enter a digit to search:</label>


<input type="number" name="digit" required>

<br>

<button type="submit">Search</button>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the input values
$number = $_POST["number"];
$digit = $_POST["digit"];

// Search for the digit in the number


$position = strpos((string)$number, (string)$digit);

if ($position !== false) {


echo "<p>The digit $digit is found at position $position in the number $number.</p>";
} else {
echo "<p>The digit $digit is not found in the number $number.</p>";
}
}
?>

</body>
</html>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem statement-8- . Design an associative, array in PHP, with ‘Course’ as key and ‘Semester’ as
value and perform following operations (using predefined functions): i) Sort this array with respect to the
key, in ascending order. ii) Sort this array with respect to the value, in descending order.
Algorithm
1. Start

2. Create an associative array with 'Course' as key and 'Semester' as value.

3. Display the original associative array.

4. Sort the array with respect to the key in ascending order:


a. Use the ksort() function.

5. Display the array after sorting by key.

6. Sort the array with respect to the value in descending order:


a. Use the arsort() function.

7. Display the array after sorting by value.

7. End
Code and Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Associative Array Operations</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

}
</style>
</head>
<body>
<h1>Associative Array Operations</h1>
<?php
// Create an associative array
$courses = array(
'Math' => 'First',
'Physics' => 'Second',
'Chemistry' => 'Third',
'Biology' => 'First',
'History' => 'Second'
);
// Display the original array
echo "<p>Original Array:</p>";
printArray($courses);
// Sort array with respect to the key in ascending order
ksort($courses);
echo "<p>Array Sorted by Key (Ascending Order):</p>";
printArray($courses);
// Sort array with respect to the value in descending order
arsort($courses);
echo "<p>Array Sorted by Value (Descending Order):</p>";
printArray($courses);

// Helper function to print the array


function printArray($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
}
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

?>
</body>

</html>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem statement :- 9 – Write a PHP script to sort strings using user defined functions.
Algorithm
1. Start
2. Create a function (e.g., customSort) that takes an array of strings as input.
a. Implement a sorting algorithm within this function.
b. The sorting algorithm can be any user-defined logic based on string comparison.
3. Take an array of strings as input from the user.
4. Call the customSort function with the array of strings.
5. Display the sorted array.
6. End
Code and Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Replacement</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
</style>
</head>
<body>

<h1>String Replacement</h1>
<?php
// Original string
$originalString = "This is a sample string. It demonstrates how 'is' is replaced.";
// Find occurrences of 'is' and replace with 'and'
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

$modifiedString = str_replace('is', 'and', $originalString);

// Display the original and modified strings


echo "<p>Original String:</p>";
echo "<p>$originalString</p>";

echo "<p>Modified String:</p>";


echo "<p>$modifiedString</p>";
?>
</body>
</html>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Problem Statement:- 10- Write a PHP script that will give the following Validations on an HTML form :
i) The name field should not be left blank.
ii) The name field should not contain numbers and special characters.
iii) Course can only be ‘MCA’, ‘BCA’ and ‘BTECH’.
iv) Email should contain ‘@’ and ‘.’ symbol.
Algorithm
1. Start

2. Create a PHP script that will be embedded in an HTML form.

3. On form submission:
a. Retrieve the values entered by the user for name, course, and email.

b. Validate the name field:


i. Check if the name field is not left blank.s
ii. Check if the name field does not contain numbers and special characters.

c. Validate the course field:


i. Check if the course is 'MCA', 'BCA', or 'BTECH'.

d. Validate the email field:


i. Check if the email contains the '@' and '.' symbols.

e. Display appropriate error messages if any validation fails.

f. If all validations pass, you can submit the form or perform further actions.

4. End
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

Final Assignment
Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>College Application Form</title>
<style>
body {
background-image: url("geu.png");
background-color: lightgray;
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}

.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 400px;
}

form {
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

display: flex;
flex-direction: column;
}

label {
margin-bottom: 8px;
font-weight: bold;
}

input, textarea {
padding: 10px;
margin-bottom: 15px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}

button {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}

.error {
color: red;
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

}
</style>
</head>
<body>
<div class="container">
<h2>College Application Form</h2>
<form action="form.php" method="post" onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<span id="nameError" class="error"></span>

<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<span id="emailError" class="error"></span>

<label for="mobile">Mobile Number:</label>


<input type="tel" id="mobile" name="mobile" required>
<span id="mobileError" class="error"></span>

<label for="address">Address:</label>
<textarea id="address" name="address" rows="2" required></textarea>
<span id="addressError" class="error"></span>

<button type="submit">Submit</button>
<br><br>
<button onclick="redirectToLink()">Home</button>

<br><br>
<button onclick="redirectTologin()">Login</button>

<script>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

function redirectToLink() {
window.location.href = 'https://geu.ac.in';
}
function redirectTologin() {
window.location.href = 'loginform from mainpage.html';
}

function validateForm() {
// Get the values from the input fields
var name = document.getElementById('name').value.trim();
var email = document.getElementById('email').value.trim();
var mobile = document.getElementById('mobile').value.trim();
var address = document.getElementById('address').value.trim();

// Validate name
if (name.length < 5) {
document.getElementById('nameError').innerText = 'Name must be at least 5 characters.';
return false;
} else {
document.getElementById('nameError').innerText = '';
}

// Validate email
if (email.length < 5) {
document.getElementById('emailError').innerText = 'Email must be at least 5 characters.';
return false;
} else {
document.getElementById('emailError').innerText = '';
}

// Validate mobile
if (mobile.length !== 10) {
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

document.getElementById('mobileError').innerText = 'Mobile number must be 10 digits.';


return false;
} else {
document.getElementById('mobileError').innerText = '';
}

// Validate address
if (address.length < 8) {
document.getElementById('addressError').innerText = 'Address should be at least 8 characters.';
return false;
} else {
document.getElementById('addressError').innerText = '';
}

// If validation passes, the form will be submitted


return true;
}
</script>
</form>
</div>
</body>
</html>
<?php
// Database connection details
$servername = "your_server_name";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Process form data if the form is submitted


if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = $_POST["name"];
$email = $_POST["email"];
$mobile = $_POST["mobile"];
$address = $_POST["address"];

// SQL query to insert data into the database


$sql = "INSERT INTO inquiry (name, email, mobile, address) VALUES ('$name', '$email', '$mobile',
'$address')";

if ($conn->query($sql) === TRUE) {


echo "Data inserted successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

// Close the database connection


$conn->close();
?>
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)
Anukalp kumar Thakur MCA-C 1st sem Roll 21 (1102699)

You might also like