C Program to check if a Number Is Even or Odd
Check if a Number is Even or Odd in C
Given an integer input, the objective is to write a code to Check if a Number is Even or Odd in C Language.
For Instance,
Input: Num = -5
Output: The number is Negative
Check if a Number is Even or Odd in C
Given an integer input, the objective is to check whether the given integer is Even or Odd. In order to do so we have the following methods:
- Method 1: Using Brute Force
- Method 2: Using Nested if-else Statements
- Method 3: Using the ternary operator
Java Program to Check Whether a Number is Even or Odd
Given an integer input, the objective is to write a Java program to check whether a number is Even or Odd.
Method 1: Using Brute Force
public class Main { public static void main(String[] args) { int number = 29; if (number % 2 == 0) System.out.println(number + " is Even"); else System.out.println(number + " is Odd"); } }
Output:
29 is Odd
Method 2: Using Ternary Operator
public class Main { public static void main (String[] args) { int number = 29; String status = number % 2 == 0 ? " is Even" : " is Odd"; System.out.println(number + status); } }
Output:
29 is Odd
Method 3: Using Bitwise Operator
public class Main { public static void main (String[] args) { int number = 29; if (isEven(number)) System.out.println("Even"); else System.out.println("Odd"); } static boolean isEven(int number) { return (!(number & 1)); } }
Output:
Odd