write the program to check whether number is odd or even
Answers
{
public static void main (int a)
{
(a/2==0)?odd:even;
system.out.print(a);
}}
Answer:
import java.util.Scanner ;
public class Numbers
{
public static boolean testEven(int n)
{
if ( n%2 = = 0)
return true;
else
return false;
}
public static void main (String args[ ] )
{
Scanner sc = new Scanner( System.in) ;
int number;
System.out.println("Enter an integer : ") ;
number = sc.nextInt( ) ;
if ( testEven(number) = = true)
System.out.println(number + "is an EVEN number") ;
else
System.out.println(number + "is an ODD number") ;
}
}
Example Output:
Enter an integer :
6
6 is an EVEN number
Enter an integer :
9
9 is an ODD number
Conditional or Selection Statements in Java:
The selection statements allow to choose the set of instructions for execution, depending upon an expression's truth value.
Java provides two types of selection statements, if and switch.
In addition, in certain circumstances, the ? operator can be used as an alternative to if statement.
The selection statements are also called conditional statements or decision statements.