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

1
2
    <html>
3
    <body>
4
        <script>
5
            var number;
6
            number = prompt("Enter the number");
7
            if (number % 2 == 0)
8
                document.write(number, " is an Even Number");
9
            else
10
                document.write(number, " is an Odd number");
11
        </script>
12
    </body>
13
    </html>
14
15
    

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 of even or odd with the input prompt

In this program, with the help of an input prompt box, the user can provide input, and then, with the help of the (%) modulus operator, the user-provided number is checked for even or odd.

  • Implicit type coercion Magic: The prompt always returns a string, but the % operator is very smart, and it internally converts the string into a number.

Why is it used?

Basic JavaScript programming condition logic:

  • To explain the concept of if-else to the students.
  • To take user inputs without defining extra HTML input elements.

Real-life Examples

  • Improve logic Building: This program helps to improve the logic skills of the students.
  • Browser-Based Quick Scripts/ Diagnostics: When you need to quickly run or diagnose the logic at the basis of small inputs.

Benefits of this Program

  • Zero HTML Setup: No need to define the HTML forms, buttons, or input boxes.
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.