Home » JavaScript

Find largest of three numbers using Nested if in JavaScript

Example of Nested if: Here, we are going to learn how to find largest of three input numbers using nested if in JavaScript?
Submitted by Pankaj Singh, on June 28, 2019

In this JavaScript example, we are using nested if to find the largest of three numbers, numbers are entering by the user from HTML page.

HTML & JS:

<!DOCTYPE html>

<HTML>
    <HEAD>
        <SCRIPT>
            function Find(){
                var a=parseInt(document.getElementById("txta").value);
                var b=parseInt(document.getElementById("txtb").value);
                var c=parseInt(document.getElementById("txtc").value);
                
                var g=0;
                
                if(a>b){
                    if(a>c){
                        g=a;
                    }
                    else{
                        g=c;
                    }
                }
                else{
                    if(b>c){
                        g=b;
                    }
                    else{
                        g=c;
                    }
                }
                
                document.getElementById("txtd").value=""+g;
                
                
            }
        </SCRIPT>
    </HEAD>
    <BODY>
        <h2>Nested If : True/False ladder</h2>
        <hr />
        <table>
            <tr>
                <td>
                    <label>Enter A</label>
                </td>
                <td>
                    <input type="text" name="txta" id="txta" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>Enter B</label>
                </td>
                <td>
                    <input type="text" name="txtb" id="txtb"  />
                </td>
            </tr>
            <tr>
                <td>
                    <label>Enter C</label>
                </td>
                <td>
                    <input type="text" name="txtc" id="txtc"  />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <input type="button" value="Find" onclick="Find()" />
                </td>
            </tr>
             
            <tr>
                <td>
                    <label>Greater</label>
                </td>
                <td>
                    <input type="text" name="txtd" id="txtd" readonly />
                </td>
            </tr>
        </table>
    </BODY>
</HTML>

Output

Find largest of three numbers using Nested if in JavaScript






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.