Algorithm
- Start
- Read three numbers (num1, num2, num3) from the user.
- Initialize a variable
largest
to store the largest number. - If
num1
is greater thannum2
andnum1
is greater thannum3
, setlargest
tonum1
. - If
num2
is greater thannum1
andnum2
is greater thannum3
, setlargest
tonum2
. - If
num3
is greater thannum1
andnum3
is greater thannum2
, setlargest
tonum3
. - Display the value of
largest
as the result. - End
Code Examples
#1 Code Example- Largest Number Among Three Numbers
Code -
Javascript Programming
// program to find the largest among three numbers
// take input from the user
const num1 = parseFloat(prompt("Enter first number: "));
const num2 = parseFloat(prompt("Enter second number: "));
const num3 = parseFloat(prompt("Enter third number: "));
let largest;
// check the condition
if(num1 >= num2 && num1 >= num3) {
largest = num1;
}
else if (num2 >= num1 && num2 >= num3) {
largest = num2;
}
else {
largest = num3;
}
// display the result
console.log("The largest number is " + largest);
Copy The Code &
Try With Live Editor
Output
Enter first number: -7
Enter second number: -5
Enter third number: -1
The largest number is -1
Enter second number: -5
Enter third number: -1
The largest number is -1
#2 Code Example- Using Math.max()
Code -
Javascript Programming
// program to find the largest among three numbers
// take input from the user
const num1 = parseFloat(prompt("Enter first number: "));
const num2 = parseFloat(prompt("Enter second number: "));
const num3 = parseFloat(prompt("Enter third number: "));
const largest = Math.max(num1, num2, num3);
// display the result
console.log("The largest number is " + largest);
Copy The Code &
Try With Live Editor
Output
Enter first number: 5
Enter second number: 5.5
Enter third number: 5.6
The largest number is 5.6
Enter second number: 5.5
Enter third number: 5.6
The largest number is 5.6
Demonstration
JavaScript Programing Example to Find the Largest Among Three Numbers-DevsEnv