Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Wednesday, February 22, 2023

JavaScript program to find the largest of three numbers

 JavaScript program to find the largest of three numbers





In JavaScript, finding the greatest number among a set of input numbers is a common task. In this blog post, we'll be discussing a JavaScript program to find the largest of three numbers.

Our program will first ask the user to input the number of numbers they want to check, and then prompt them to enter those numbers one by one. Using an if..else conditional statement, the program will compare each input number to the previous greatest number found and update the greatest number variable accordingly.

We'll be using the prompt() function to accept input from the user, and the alert() function to output the result. By the end of this post, you'll have a working JavaScript program that can check the greatest number among a set of input numbers.

Write a JavaScript program to find the largest of three numbers.

JavaScript program to find the largest of three numbers


 

Explanation:

i) We first use the prompt() function to ask the user to input three numbers. The prompt() function displays a dialog box with an input field for the user to enter a value. In this case, we ask the user to input three numbers and store each number in a separate variable using the let keyword.


ii) We use the parseInt() function to convert the user's input, which is in string format, to a number. This ensures that we can perform mathematical operations on the input values.


iii) We use an if-else statement to check which of the three input numbers is the greatest. The if statement checks if num1 is greater than or equal to both num2 and num3. If this is true, we display num1 as the greatest number using the alert() function. The else if statement checks if num2 is greater than or equal to both num1 and num3. If this is true, we display num2 as the greatest number using the alert() function. If neither of these conditions is true, we display num3 as the greatest number using the alert() function.


iv) The program displays the result to the user using the alert() function. The alert() function displays a dialog box with a message and an OK button. The message in this case is the greatest number among the three input values.

 

Write a JavaScript program to find the largest of three numbers using function.




 

Explanation:

i) We define a function called findGreatestNumber() that takes three parameters: num1, num2, and num3. The function uses an if-else statement to check which of the three input numbers is the greatest, and returns the result.


ii) We use the prompt() function to ask the user to input three numbers and store them in separate variables using the let keyword.


iii) We call the findGreatestNumber() function and pass the three input numbers as arguments.


iv) The findGreatestNumber() function calculates the greatest number among the three input numbers using an if-else statement and returns the result.

v) We store the result of the findGreatestNumber() function in a variable called result.


vi) We use the alert() function to display the result in an alert box. The alert() function displays a dialog box with a message and an OK button. The message in this case is the greatest number among the three input values, which is stored in the result variable.



Output:




Conclusion:

We have learned how to write a JavaScript program to find the largest of three numbers. By understanding the logic behind this program, you can apply similar techniques to solve various programming problems in JavaScript.

You may also read:

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages