Home » JavaScript

JavaScript code to find largest of three numbers

This JavaScript code will read three numbers in a text box and it will return the largest number from given/input three numbers.
Submitted by Aleesha Ali, on March 25, 2018

Given three numbers and we have to find its largest number using JavaScript.

JavaScript code to find largest of three numbers

<script>
	function largest()
	{
		var num1, num2, num3;
		num1 = Number(document.getElementById("N").value);
		num2 = Number(document.getElementById("M").value);
		num3 = Number(document.getElementById("O").value);

		if(num1>num2 && num1>num3)
		{
			window.alert(num1+"-is greatest");
		}
		else if(num2>num1 && num2>num3)
		{
			window.alert(num2+"-is greatst");
		}
		else if(num3>num1 && num3>num1)
		{
			window.alert(num3+"is greatest");
		}
	}
</script>

Calling JavaScript function in HTML (HTML, JS Code)

<html>
	<head>
		<script>
			function largest()
			{
				var num1, num2, num3;
				num1 = Number(document.getElementById("N").value);
				num2 = Number(document.getElementById("M").value);
				num3 = Number(document.getElementById("O").value);

				if(num1>num2 && num1>num3)
				{
					window.alert(num1+"-is greatest");
				}
				else if(num2>num1 && num2>num3)
				{
					window.alert(num2+"-is greatst");
				}
				else if(num3>num1 && num3>num1)
				{
					window.alert(num3+"is greatest");
				}
			}
		</script>
	</head>
	<body>
		<h1>Calculate largest among three numbers</h1>
		<hr color="cyan">
		<br>
		Enter number 1: <input type="text" id="N"></input><br>
		Enter number 2: <input type="text" id="M"></input><br>
		Enter number 3: <input type="text" id="O"></input><br>
		<hr color="cyan">
		<center><button onclick="largest()">OK</button>
	</body>
</html>

Output

JavaScript code to find largest of three numbers





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.