Scala Program to Check Whether a Number is Even or Odd

Posted in

Sangeeta Gulia
Last updated on January 13, 2023

    We all have been very much familiar with even and odd numbers since childhood. Wouldn’t it be cool if you learned to write a program that detects whether a number is even or odd? If you are learning any new programming language, this program serves as an entry-level practice problem. So, if you are learning Scala and practicing it, this article would be beneficial.

    In this article, we will help you write a Scala program to find whether a number is odd or even. Though you know what even and odd numbers are, let us recollect them once before proceeding.

    What are Even Numbers?

    Even numbers are the numbers that can be completely divided by 2 and produces the remainder 0.

    Examples of even numbers include 0, 2, 4 ,6 ,8 , 10, …….

    Trick to identify if a number is even or not: Even numbers always have either 0, 2, 4, 6, or 8 as their last digit.

    What are Odd Numbers?

    Odd numbers are numbers that can not be completely divided by 2. When divided by 2, odd numbers leave 1 as their remainder.

    Examples of odd numbers include 1, 3, 5, 7, 9, 11, 13, 15, …….

    Trick to identify if a number is odd or not: Odd numbers always have either 1, 3, 5, 7, or 9 as their last digit.

    Scala Program to Check Whether a Number is Even or Odd

    With the above details in mind, let us begin writing our program. We will be using the conditional statement construct of Scala: If Else ladder.

    Program

    objectEvenOrOddextendsApp {
    
    println("Please enter an integer number")
    val inputNumber = scala.io.StdIn.readInt()
    
    if (inputNumber % 2 == 0)
    println("The entered number is even")
    else
    println("The entered number is odd")
    
    }

    Output

    Sample Output 1: Positive Integer Number

    Please enter an integer number
    5
    The entered number is odd

    Sample Output 2: Negative Integer Number

    Please enter an integer number
    -20
    The entered number is even

    Sample Output 3: Zero Check: Even or Odd

    Please enter an integer number
    0
    The entered number is even

    Explanation

    In the above program, we used the method readInt() from the StdIn object present in the scala.io package. After the user enters the input on the console from the keyboard, it is stored in the variable named inputNumber .

    As per the rules we discussed above to identify if a number is even or not, we used the modulus function present in the Int.scala class to find out the remainder after division by 2. If it is completely divisible by 2, the number is printed as an even number; otherwise, it is printed as an odd number.

    Conclusion

    Now, you know how to write a Scala program to check whether a number is even or odd. You can try running the same program and try out the results for different integer numbers.

    Stay connected to our blogs for more such interesting programs.

    Keep Coding, Keep Learning!

    Leave a Comment on this Post

    0 Comments