Write a program to check the given number is even or odd.

1
2
    <!DOCTYPE html>
3
    <html lang="en">
4
        
5
        
6
    <head>
7
        <meta charset="UTF-8">
8
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
9
        <title>Even or Odd Checker</title>
10
        <style>
11
            body {
12
                font-family: Arial, sans-serif;
13
                margin: 20px;
14
                padding: 20px;
15
                border: 1px solid #ccc;
16
                border-radius: 5px;
17
                max-width: 400px;
18
            }
19
        
20
        
21
            #result {
22
                margin-top: 20px;
23
                font-size: 1.2em;
24
                color: #333;
25
            }
26
        </style>
27
        <script>
28
            function even() {
29
                var n = document.getElementById("val").value;
30
        
31
        
32
                // Check if the input is empty or not a number
33
                if (n === "" || isNaN(n)) {
34
                    document.getElementById("result").innerHTML = "Please enter a valid number.";
35
                    return;
36
                }
37
        
38
        
39
                // Convert the input to a number
40
                n = Number(n);
41
        
42
        
43
                // Determine if the number is even or odd
44
                if (n % 2 === 0) {
45
                    document.getElementById("result").innerHTML = n + " is an even number.";
46
                } else {
47
                    document.getElementById("result").innerHTML = n + " is an odd number.";
48
                }
49
            }
50
        </script>
51
    </head>
52
        
53
        
54
    <body>
55
        
56
        
57
        <h3>Check if a Number is Even or Odd</h3>
58
        <label for="val">Enter a number:</label>
59
        <input type="number" id="val" aria-describedby="numberHelp">
60
        <button type="button" onclick="even()">Check</button>
61
        <hr>
62
        <p id="result" aria-live="polite"></p>
63
    </body>
64
        
65
        
66
    </html>
67
68
    

Output

1
2
        No Output Available
3
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

Core concept: Whether the user-given number is even or odd

This program is a modern and robust web-based utility tool. The user provides their input and presses the check button; then the JS user-defined event function is called.

Validation & Conversion: Input validation is applied in this program, and if the user input is correct, then the module % program gives the result of whether the number is even or not.

Why is it used?

  • This is the standard and best-practice approach to take input from the user.

Real-life Applications

  • Interactive educational portals: In math tools or programming utility websites, where students can enter their roll number to get the instant result of their exam.
  • Data validation Form: To check mathematical numbers or number parity.

Benefits of this Program

  • Robust Error Handling: If the user provides invalid data or an empty field, then this will check the user input and give the error message to the user.
  • Improve UX: Proper validations and error messages shown on screen cause a better user experience.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.