Computer ScienceNEB 2082
Write JavaScript code to input any three numbers and find the smallest number among them. [5] OR Write a PHP script to connect to a MySQL database. [5]
5Answer
JavaScript Solution (Finding the Smallest of Three Numbers)
// JavaScript code to input three numbers and find the smallest
let num1 = parseFloat(prompt("Enter first number:"));
let num2 = parseFloat(prompt("Enter second number:"));
let num3 = parseFloat(prompt("Enter third number:"));
// Check which number is smallest
if (num1 <= num2 && num1 <= num3) {
smallest = num1;
} else if (num2 <= num1 && num2 <= num3) {
smallest = num2;
} else {
smallest = num3;
}
// Display the result
document.write("The smallest number among " + num1 + ", " + num2 + ", and " + num3 + " is: <b>" + smallest + "</b>");
How it works:
- The script uses
prompt()to take three numeric inputs from the user. - It converts the inputs to floating-point numbers using
parseFloat()to handle decimal values. - Three
if-elseconditions compare the numbers to determine the smallest:- First checks if
num1is the smallest. - If not, checks if
num2is the smallest. - If neither is smallest,
num3must be the smallest.
- First checks if
- The result is displayed using
document.write()with the smallest number highlighted in bold.
PHP Solution (Connecting to MySQL Database)
<?php
// PHP script to connect to MySQL database
$servername = "localhost"; // Replace with your server if different
$username = "root"; // Replace with your MySQL username
$password = ""; // Replace with your MySQL password
$dbname = "test_db"; // Replace with your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "<p style='color:green;'>Connected successfully to MySQL database!</p>";
}
// Close connection
$conn->close();
?>
How it works:
- The script defines four variables:
$servername: Hostname (default:localhost).$usernameand$password: MySQL credentials (replace with actual values).$dbname: Name of the database to connect to.
- It creates a connection using
new mysqli(). - The
if-elseblock checks if the connection succeeded:- If
connect_errorexists, it displays the error message. - If successful, it prints a green success message.
- If
- The connection is closed using
$conn->close()to free resources.
Discussion
Loading…
More Computer Science questions
Which of the following is the purpose of using primary key in database ? a) To uniquely identify a record b) To store duplicate record c) To backup data d) To…NEB 2082 (MCQs)1A company needs to modify an existing table by adding a new column for employee email addresses. For this, which SQL command should be used ? a) CREATE b)…NEB 2082 (MCQs)1Which protocol is used for secure communication over a computer network ? a) HTTP b) FTP c) HTTPS d) TelnetNEB 2082 (MCQs)1Which control structure in JS (Java Script) is used to execute a block of code repeatedly based on a given condition ? a) For loop b) if else c) switch case…NEB 2082 (MCQs)1Select the invalid variable name in PHP. a) $name b) $ name c) $1name d) $name123NEB 2082 (MCQs)1The statement int number ( int a); is a ... a) function call b) function definition c) function declaration d) function executionNEB 2082 (MCQs)1