iseven

iseven(x::Integer) -> Bool

Returns true is x is even (that is, divisible by 2), and false otherwise.

julia> iseven(9)
false

julia> iseven(10)
true

Examples

  1. Check if a number is even:

    julia> iseven(9)
    false
    
    julia> iseven(10)
    true

    This example demonstrates how to use the iseven function to check if a given number is even or not.

  2. Use in conditional statements:

    julia> x = 17;
    julia> if iseven(x)
              println("The number is even.")
          else
              println("The number is odd.")
          end
    The number is odd.

    The iseven function can be used in conditional statements to perform different actions based on whether a number is even or odd.

  3. Check evenness for a range of numbers:
    julia> numbers = [1, 2, 3, 4, 5, 6];
    julia> even_numbers = filter(iseven, numbers)
    3-element Array{Int64,1}:
    2
    4
    6

    In this example, the iseven function is used with the filter function to extract only the even numbers from a given array.

Common mistake example:

julia> iseven(3.5)
ERROR: MethodError: no method matching iseven(::Float64)

The iseven function only works with integers (Integer type) and not with floating-point numbers. Make sure to use it with the correct data type.

See Also

digits, inf, isdigit, iseven, isfinite, isless, islower, isnumber, isodd, isprime, isqrt, issorted, issubnormal, isxdigit, nan,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: