//Square that Number
function square(num) {
var result = num * num;
console.log(result);
}
// Example usage:
var userInput = parseInt(prompt("Enter a number: "));
if (isNaN(userInput)) {
console.log("Invalid input. Please enter a valid number.");
} else {
square(userInput);
}
//Multiplying 2 Numbers
function multiply(num1, num2) {
var result = num1 * num2;
console.log(result);
}
// Example usage:
var input1 = parseFloat(prompt("Enter first number: "));
var input2 = parseFloat(prompt("Enter second number: "));
if (isNaN(input1) || isNaN(input2)) {
console.log("Invalid input. Please enter valid numbers.");
} else {
multiply(input1, input2);
}
//Calculating the Average
function calculateAverage() {
var total = 0;
var count = arguments.length;
for (var i = 0; i < count; i++) {
total += arguments[i];
}
var average = total / count;
console.log("The calculated average is: " + average.toFixed(2));
}
// Example usage:
var input1 = parseFloat(prompt("Enter the first number: "));
var input2 = parseFloat(prompt("Enter the second number: "));
var input3 = parseFloat(prompt("Enter the third number: "));
if (isNaN(input1) || isNaN(input2) || isNaN(input3)) {
console.log("Invalid input. Please enter valid numbers.");
} else {
calculateAverage(input1, input2, input3);
}
//Check that Number
function checkNumber(number) {
if (number > 0) {
console.log("Positive");
} else if (number < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
}
// Example usage:
var userInput = parseFloat(prompt("Enter a number: "));
if (isNaN(userInput)) {
console.log("Invalid input. Please enter a valid number.");
} else {
checkNumber(userInput);
}
//Printing Patterns
function printPattern(rows) {
for (var i = 1; i <= rows; i++) {
var line = '';
for (var j = 1; j <= i; j++) {
line += '*';
}
console.log(line);
}
}
// Example usage:
var userInputRows = parseInt(prompt("Enter the number of rows: "));
if (isNaN(userInputRows) || userInputRows < 1) {
console.log("Invalid input. Please enter a valid number of rows.");
} else {
printPattern(userInputRows);
}
//BMI Calculation
function calculateBMI(weight, height) {
var bmi = weight / (height * height);
console.log("BMI: " + bmi.toFixed(2));
}
// Example usage:
var userInputWeight = parseFloat(prompt("Enter your weight in kilograms: "));
var userInputHeight = parseFloat(prompt("Enter your height in meters: "));
if (isNaN(userInputWeight) || isNaN(userInputHeight) || userInputWeight <= 0 ||
userInputHeight <= 0) {
console.log("Invalid input. Please enter valid weight and height values.");
} else {
calculateBMI(userInputWeight, userInputHeight);
}