JavaScript program to find the largest of three numbers
Write a 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:
No comments:
Post a Comment