In: Computer Science
You are required to write an interactive JS program that prompts the user for three numbers and then finds the sum, average, smallest, and the largest value of the numbers and prints the results labeling properly.
Executable
Code:
// reading user input and parsing them to integers
num1 = parseInt(prompt("Enter the first number: "));
num2 = parseInt(prompt("Enter the second number: "));
num3 = parseInt(prompt("Enter the third number: "));
// calculating the sum
sum = num1 + num2 + num3;
// calculating the average
avg = sum / 3;
// determining the smallest
if (num1 < num2 && num1 < num3) {
smallest = num1;
}
else if (num2 < num3 && num2 < num1) {
smallest = num2;
}
else {
smallest = num3;
}
// determining the largest
if (num1 > num2 && num1 > num3) {
largest = num1;
}
else if (num2 > num3 && num2 > num1) {
largest = num2;
}
else {
largest = num3;
}
// displaying the results
console.log("Sum of the three numbers is:", sum);
console.log("Average of the three numbers is:", avg);
console.log("Smallest of the three numbers is:", smallest);
console.log("Largest of the three numbers is:", largest);
Sample
Output:
Please don't hesitate to contact me if you have any
queries or require any modifications :)
Do rate the answer if it was helpful
thanks.