Conditional Statement to Find Largest of 5 Numbers in JavaScript

JavaScript program to find largest of 5 number using conditional statement. First you know that what is conditional statement, Some overview of the conditional statement in below.


conditional-statement-find-largest-five-number-javascript-decoderp


Conditional Statement :

In the conditional statement, combination of two words, one is conditional and other is statement. Conditional means condition as like a > b, a <  b, a <= b, a >= b, a != b, a == b. Statement means syntax or program. The conditional statement, some time single condition and single statement is there . or some time more then one conditions and more then one statement are in there. If condition is satisfy, then under the condition those statement are there, this statement are executed. Otherwise check to the other condition.

Some conditional statements are : if, else, else-if, nested-if condition etc.

Algorithm :

Step:1 Start.
Step:2 Read five numbers a, b, c, d, e.
Step:3 If a > b and a > c and a > d and a > e then print a is greatest. If b > a and b > c and b > d and b > e then print b is greatest. If c > a and c > b and c > d and c > e then print c is greatest. If d > a and d > b and d > c and d > e then print d is greatest. Else e is greatest.
Step:4 Stop.

JavaScript code :

<HTML>
  <HEAD>
    <TITLE>
    </TITLE>
  </HEAD>
  <BODY>
    <SCRIPT>
     var a = prompt("Enter 1st no : ");
     var b = prompt("Enter 2nd no : ");
     var c = prompt("Enter 3rd no : ");
     var d = prompt("Enter 4th no : ");
     var e = prompt("Enter 5th no : ");
     if(a>b && a>c && a>d && a>e)
        alert(a+" is greatest no.");
     else if(b>a && b>c && b>d && b>e)
        alert(b+" is greatest no.");
     else if(c>a && c>b && c>d && c>e)
        alert(c+" is greatest no.");
     else if(d>a && d>b && d>c && d>e)
        alert(d+" is greatest no.");
     else
        alert(e+" is greatest no."); 
    </SCRIPT>
  </BODY>
</HTML>

Output :

Enter 1st no :
12
Enter 2nd no :
34
Enter 3rd no :
56
Enter 4th no :
22
Enter 5th no :
10

56 is greatest no.


Explanation:

In this code, JavaScript are used inside the html code. It means we not write separate code for both html and javascript code. If you understand algorithm of this code then not required of this explanation. we write javascript code, inside the body of the html. 'var' is a keyword, it is used to declare the variable. It means, if we can declare 'var' data type then can not  required to declare other data type as like int, float, char etc.  'prompt'  is a keyword, it is used to receive the user input. 'alert' is a keyword, it is used to display the output in the alart box .

Post a Comment

0 Comments