JavaScript program to Find largest of Two numbers

In this program, you will take 2 inputs from users using a window prompt, and you will find the largest number.

Program for largest of two number

<!DOCTYPE html>
    function Largest2No() {
	  var n1,n2;
	  n1 = parseInt(prompt("enter first number:"));
	  n2 = parseInt(prompt("enter second number:"));
           document.write(n1+" is Largest number");
	   document.write(n2+" is Largest number");
      <input type="button" value="LargestNo" onclick="Largest2No()" />
Run now

Input has taken through javascript prompt method


enter first number:14
enter second number:12
14 is Largest number

Logic to find the Largest of two numbersĀ 

  • when the user clicks on the 'LargestNo' button, the Largest2No() function will get called.
  • In the Largest2No() function, Take two numbers from the user and stored them in n1 and n2.
  • compare n1 and n2 by using the if statement i.e if(n1>n2){ }, if n1 is greater than n2 then if statement will get executed. and you will print "n1 is the largest number".
  • if n1 is less than n2 then else part will get executed and hence you will print "n2 is the largest number".