Technology Programming

Even Function in Java

    Variables

    • If a program needs to determine whether or not a number variable is even, it can do so using variables. The following sample Java code instantiates two variables:
      int number = 6;
      boolean isEven=false;

      The number is modeled as a primitive type integer variable with an arbitrary initial value. The boolean variable can only have a value of either true or false. The boolean is initialized to false as its default value. The boolean variable name is a meaningful representation of its purpose and meaning. After the arithmetic process, the value will be true if the number is even, false if it is not.

    Remainder

    • Java programs can use a range of arithmetic operators. The remainder operator returns a value representing the remainder after a division calculation completes. For example, the following calculation would result in a value of five:
      15%10

      Once the first operand has been divided by the second, the remainder is five. A program can use the following code to establish the remainder of the number variable after dividing by two:
      number%2

      The calculation divides by two because even numbers divide by two with no remainder, while odd numbers leave a remainder of one when divided by two.

    Conditional

    • By incorporating the remainder calculation into a conditional statement, a Java program can update the value of the boolean variable. The following code demonstrates:
      if((number%2)==0) isEven=true;

      This code carries out a test. If the remainder of the number, after dividing by two, is equal to zero, it must be even. If the test returns true, the boolean variable is therefore updated to reflect a true value. If the conditional test returns a false result, meaning the number is not even, it has to be odd. In this case the program does not need to do anything because the boolean variable still has its initial value of false. The program could test the function as follows:
      System.out.println(number + " is even? - " + isEven);

    Control

    • Once a Java program has a boolean variable with a value that represents whether or not the number is even, it can use that variable to dictate control flow. The following sample code demonstrates:
      if(isEven) {
      //processing for even numbers
      }
      else {
      //processing for odd numbers
      }

      Inside each of these blocks, the program can dictate processing for each case, the number being even or odd. This structure allows programmers to tailor what happens when a program executes to specific circumstances.

Related posts "Technology : Programming"

The Importance Of Having a WordPress Business Theme

Programming

Website Design Is Necessary For Your Website

Programming

The Most effective On line Paid Survey Evaluation

Programming

Adelaide SEO - Links And Keywords, How Should They Be Used

Programming

C Programming Compilers for Microcontrollers

Programming

How Should A DJ Make Music Logo That Is Distinct And Cool?

Programming

Call to Action Concepts for Small Businesses

Programming

Microsoft Access Databases in Office 365

Programming

Why web design is crucial for producing world class websites

Programming

Leave a Comment