In: Computer Science
JAVA PROGRAMMING
source code using appropriate programming style (e.g., descriptive variable names, indenting, comments, etc.).
1. Using the remainder (%) operator, write a program that inputs an integer and displays if the number is even or odd.
B. Write the same program using some other technique without using the remainder (%) operator.
Java Program:
(1)
import java.util.*;
class EvenOrOdd_1
{
public static void main(String[] args)
{
int n;
//Scanner class object
Scanner reader = new
Scanner(System.in);
//Reading n value from user
System.out.print("Enter n:
");
n = reader.nextInt();
//Checking for even or odd using %
operator
if(n%2 == 0)
{
System.out.printf(" %d is even ", n);
}
else
{
System.out.printf(" %d is odd ", n);
}
}
}
Sample Run:

_______________________________________________________________________________________________________
(B)
import java.util.*;
class EvenOrOdd_2
{
public static void main(String[] args)
{
int n;
//Scanner class object
Scanner reader = new
Scanner(System.in);
//Reading n value from user
System.out.print("Enter n:
");
n = reader.nextInt();
//Checking for even or odd using /
and * operator
if( (n/2)*2 == n)
{
System.out.printf(" %d is even ", n);
}
else
{
System.out.printf(" %d is odd ", n);
}
}
}
Sample Run:
