C Program to Check Whether a Given Number is Even or Odd

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

We are given a number. We need to determine whether the given number is an odd or even number.

Even Numbers: All numbers divisible by 2 are called even numbers. In other words, all numbers that give remainder as 0 on division by 2 are called even numbers.

Odd Numbers: All numbers that are not completely divisble by 2 are called odd numbers. In other words, all numbers that give a non-zero remainder on division by 2 are called odd numbers.

EVEN ODD 1

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction

Input Format: The input to our even-odd program in C consists of a single number that we want to categorise as even or odd.

Output Format: Print "Even" (without quotes) if the input number is even. Print "Odd" (without quotes) if the given input number is odd.

Example 1:

This is because 5 on division by 2 gives remainder 1.

Example 2:

This is because 10 on division by 2 gives the remainder 0.

C Program to Check Even or Odd

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Using Modulus Operator

  • The first way to code even odd program in C is by using modulus ( % ) operator in C. % Operator produces the remainder of integer division .If a and b are integers then a%b will given us the remainder when a is divided by b.

  • If b divides a completely then the remainder is 0 otherwise we will get remainder in the range [1 to a-1].

  • In this odd program, we will find ** input number % 2 **. If we get 0 as the remainder, it means our number is completely divisible by 2 tand hence is is an even number. And if we get a non-zero remainder, it means our number is not completely divisible by 2; hence, the given number is odd.

Code:

Output:

Time Complexity: O(1)

Auxiliary Space: O(1)

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Using Bitwise Operator

Here in this even odd program in C, we will use bitwise operator. If we consider our number in binary then we can easily understand that if our last bit is 1 then our number is odd otherwise it is even. This is because least significant bit of all odd numbers is set.

EVEN ODD 2

EVEN ODD 3

So to check whether our last bit is 1 or not we will do bitwise AND (&) of our given number with 1. If the last bit will be 1, we will get 1 otherwise we will get 0.

Truth table for AND operator EVEN ODD 4

For Example

Code

Output:

Time Complexity: O(1)

Auxiliary Space: O(1)

Program to Check Even or Odd Using the Ternary Operator

The ternary operator, also known as the conditional operator, is similar to the if-else statement as it follows the same method, but the conditional operator takes up less space and allows you to write if-else statements in the quickest possible time.

Syntax of Ternary operator:

(expression_1) ? (expression_2) : (expression_3);

expression_1 refers to the condition to be checked . If expression 1 evaluates to true then expression_2 is executed else expression_3 is executed.

It's similar to :

EVEN ODD 5

EVEN ODD 6

Here in this odd even program , we check whether input number % 2 is 0 or not . Then using conditional operator output the desired result.

Code:

Output:

Time Complexity: O(1)

Auxiliary Space: O(1)

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

C Program to Check Odd or Even Without Using Bitwise Or Modulus Operator

Input : A single number n.

In this odd-even program, we will check whether the given input is odd or even by using a loop. We will initialise a variable x to 1 initially, and then we will run our loop n a number of times, and each time, we will complement our variable x (meaning if it was 1, then it will now become 0, and if it was 0, it will become 1 now).

Now, if our variable x is 1 at the end of the loop, then we can say that our number is even; otherwise, our number is odd.

Code :

Output:

Time Complexity: O(n)

Auxiliary Space: O(1)

Conclusion

  • Even Numbers: All those numbers that are completely divisible by 2 are called even numbers.

  • Odd Numbers : All those numbers that are not completely divisble by 2 are called odd numbers.

  • a%b returns the remainder when a is divisble by b. If b divides a completely we get remainder as 0. Otherwise we will get a number between [1 to a-1].

  • The last bit of a number in binary representation decides whether the number is odd or even. If the last bit is 1, then the number odd and if the last bit is 0 then the number is even.

  • Ternary Operator also known as conditional operator is similar to if-else statements.

  • Syntax of Ternary Operator is (expression_1) ? (expression_2) : (expression_3);