Signup/Sign In
PUBLISHED ON: MARCH 13, 2023

JavaScript Program to Find the Largest Among Three Numbers

In this tutorial, we'll learn how to make a JavaScript programme that looks at three numbers and finds the biggest one. Comparing a set of numbers and finding the one with the highest value is a common operation that is often used in math and programming. It's an easy job that can be done with a few lines of code. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

1. Using Math.max() Function

JavaScript built-in Math.max() function to find the largest among the numbers.

Program to Find the Largest Among Three Numbers

// Program to find the largest among the three numbers

var num1 = parseInt(prompt("Enter first number: "));
var num2 = parseInt(prompt("Enter second number: "));
var num3 = parseInt(prompt("Enter third number: "));
var largest;

//conditions
if(num1 >= num2 && num1 >= num3) 
    largest = num1;

else if (num2 >= num1 && num2 >= num3) 
    largest = num2;

else 
    largest = num3;

console.log("The largest number is " + largest);


Enter first number: 52
Enter second number: 55
Enter third number: 65
The largest number is 65

2. Finding Largest Number using if ... else Statement

We can find the largest among the three numbers using the if...else statement.

Logic:

The numbers are compared with one another using the greater and equal operator. The if else if.. statement is used to check the condition. Whenever we need to check two conditions we use the && Operator.

  • N1>N2 && N1>N3 then greatest N1.
  • N2>N1 && N2>N3 then greatest N2.
  • N3>N1 && N3>N2 then greatest N3.

Program to Find the Largest Among Three Numbers

// Program a in JavaScript to find the largest among three numbers

const num1 = parseInt(prompt("Enter first number: "));
const num2 = parseInt(prompt("Enter second number: "));
const num3 = parseInt(prompt("Enter third number: "));
let largest;

//conditions
if(num1 >= num2 && num1 >= num3) 
    largest = num1;

else if (num2 >= num1 && num2 >= num3) 
    largest = num2;

else 
    largest = num3;

console.log("The largest number is " + largest);


Enter first number: 52
Enter second number: 55
Enter third number: 65
The largest number is 65

Conclusion

In conclusion, we have learned how to create a JavaScript program that finds the largest among three given numbers. By using a series of conditional statements, we were able to compare the values and determine the maximum. This program can be useful in a variety of applications where we need to perform basic arithmetic operations and find the maximum value among a set of numbers. It is a simple but powerful tool that can help us to solve problems and write more efficient code.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.