
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to print “Even” or “Odd” without using Conditional statement
In this section we will see how to check whether a number is odd or even without using any kind of conditional statements like (<, <=, !=, >, >=, ==).
We can easily check the odd or even by using the conditional statements. We can divide the number by 2, then check whether the remainder is 0 or not. if 0, then it is even. Otherwise we can perform AND operation with the number and 1. If the answer is 0, then it is even, otherwise odd.
Here no conditional statements can be used. We will see two different methods to check the odd or even.
Method 1
Here we will create an array of strings. The index 0 position will hold “Even”, and index 1 position will hold “Odd”. We can send the remainder after dividing the number by 2 as index to get the result directly.
Example Code
#include<stdio.h> main() { int n; char* arr[2] = {"Even", "Odd"}; printf("Enter a number: "); //take the number from the user scanf("%d", &n); printf("The number is: %s", arr[n%2]); //get the remainder to choose the string }
Output 1
Enter a number: 40 The number is: Even
Output 2
Enter a number: 89 The number is: Odd
Method 2
This is the second method. In this method we will use some tricks. Here logical and bitwise operators are used. At first we are performing AND operation with the number and 1. Then using logical and to print the odd or even. When the result of bitwise AND is 1, then only the logical AND operation will return the odd result, otherwise it will return even.
Example Code
#include<stdio.h> main() { int n; char *arr[2] = {"Even", "Odd"}; printf("Enter a number: "); //take the number from the user scanf("%d", &n); (n & 1 && printf("odd"))|| printf("even"); //n & 1 will be 1 when 1 is present at LSb, so it is odd. }
Output 1
Enter a number: 40 even
Output 2
Enter a number: 89 odd
- Related Articles
- C++ Program to Print “Even” or “Odd” without using conditional statement
- C++ Program to Check Whether Number is Even or Odd
- Java program to Print Odd and Even Number from an Array
- C Program to Check if count of divisors is even or odd?
- Maximum of four numbers without using conditional or bitwise operator in C++
- C program to print characters without using format specifiers
- Java program to print odd and even number from a Java array.
- How to print a name multiple times without loop statement using C language?
- C Program for Print individual digits as words without using if or switch.
- Program to print root to leaf paths without using recursion using C++
- C Program to print “Hello World!” without using a semicolon
- C program to print number series without using any loop
- Java program to find whether given number is even or odd
- Java Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Even or Odd
