# Odd or Even Number

I recently gave a single-class project to my Computer Science I students with the following guidelines:

• The user is asked to enter a valid integer between -2,147,483,647 and 2,147,483,647
• The program will then clearly output whether the entered number is an odd number or an even number
• Following the calculation, the user will be asked if they want to run the program again or exit

The odd/even calculation can be run similar to the following:

```//Program Name: Blue Pelican Java Lesson 09 (Project)
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class project9
{
public static void main (String[] args){
Scanner numberInput = new Scanner(System.in);
System.out.print("Enter an integer up to 2,147,483,647: _ ");
int numberOddEven = numberInput.nextInt();
double check = numberOddEven % 2;
if (check == 0){
System.out.println("The integer " + numberOddEven + " is even");
}else{
System.out.println("The integer " + numberOddEven + " is odd");
}
}
}```

This will run the calculation only once. It is using a conditional statement based upon modulus division. If the number entered is an even number, when it is divided by 2 the modulus (remainder) is 0. If the number entered is an odd number, when it is divided by 2, the modulus (remainder) is 1. From here, this is a simple boolean statement (line 18).

Now, we’ll look at creating the loop:

```//Program Name: Blue Pelican Java Lesson 09 (Project)
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class project9
{
public static void main (String[] args){
int runAgain = 1;
while (runAgain == 1){
//* * * * * * * * * * * * * * * * * * * * *
// LINES 13 TO 21 FROM ABOVE BLOCK OF CODE
//* * * * * * * * * * * * * * * * * * * * *
Scanner runItAgain = new Scanner(System.in);
System.out.println("");
System.out.println("Do You Want to Run Again?");
System.out.println("1 - Yes, Run Again");
System.out.println("2 - No, Exit Program");
runAgain = runItAgain.nextInt();
}
System.out.println("Exiting Program. Thank you.");
}
}```

As you can see, we chose to employ a while loop. The initial variable was declared on line 13, which allows us to enter the loop on line 14.

Lines 18 through 22 are asking the user is the want to run the program again. Line 23 assigns the value that was entered to the “runAgain” variable. If the value entered is 1, the while loop runs again. If it is anything other than 1, the loop is exited and line 25 runs.

The final program could look similar to the following:

```//Program Name: Blue Pelican Java Lesson 09 (Project)
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class project9
{
public static void main (String[] args){
int runAgain = 1;
while (runAgain == 1){
Scanner numberInput = new Scanner(System.in);
System.out.print("Enter an integer up to 2,147,483,647: _ ");
int numberOddEven = numberInput.nextInt();
double check = numberOddEven % 2;
if (check == 0){
System.out.println("The integer " + numberOddEven + " is even");
}else{
System.out.println("The integer " + numberOddEven + " is odd");
}
Scanner runItAgain = new Scanner(System.in);
System.out.println("");
System.out.println("Do You Want to Run Again?");
System.out.println("1 - Yes, Run Again");
System.out.println("2 - No, Exit Program");
runAgain = runItAgain.nextInt();
}
System.out.println("Exiting Program. Thank you.");
}
}```