Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 57141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:38:41+00:00 2026-05-10T17:38:41+00:00

How can I check if a given number is even or odd in C?

  • 0

How can I check if a given number is even or odd in C?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:38:42+00:00Added an answer on May 10, 2026 at 5:38 pm

    Use the modulo (%) operator to check if there’s a remainder when dividing by 2:

    if (x % 2) { /* x is odd */ } 

    A few people have criticized my answer above stating that using x & 1 is ‘faster’ or ‘more efficient’. I do not believe this to be the case.

    Out of curiosity, I created two trivial test case programs:

    /* modulo.c */ #include <stdio.h>  int main(void) {     int x;     for (x = 0; x < 10; x++)         if (x % 2)             printf('%d is odd\n', x);     return 0; }  /* and.c */ #include <stdio.h>  int main(void) {     int x;     for (x = 0; x < 10; x++)         if (x & 1)             printf('%d is odd\n', x);     return 0; } 

    I then compiled these with gcc 4.1.3 on one of my machines 5 different times:

    • With no optimization flags.
    • With -O
    • With -Os
    • With -O2
    • With -O3

    I examined the assembly output of each compile (using gcc -S) and found that in each case, the output for and.c and modulo.c were identical (they both used the andl $1, %eax instruction). I doubt this is a ‘new’ feature, and I suspect it dates back to ancient versions. I also doubt any modern (made in the past 20 years) non-arcane compiler, commercial or open source, lacks such optimization. I would test on other compilers, but I don’t have any available at the moment.

    If anyone else would care to test other compilers and/or platform targets, and gets a different result, I’d be very interested to know.

    Finally, the modulo version is guaranteed by the standard to work whether the integer is positive, negative or zero, regardless of the implementation’s representation of signed integers. The bitwise-and version is not. Yes, I realise two’s complement is somewhat ubiquitous, so this is not really an issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I check the given email id is valid or not using ELIPS
How can I check if the user has given permission and not disabled (publish_action)
I have to check, if given number is divisible by 7, which is usualy
Can check out here alt text http://51hired.com/static/problem.bmp
I can check for iPhone with this code: (navigator.userAgent.match(/iPhone/i)) But I want to target
How I can check the date using a calender , what I want to
With this code I can check the caret position in a textarea in firefox:
I know that I can check whether the linux timestamp in $time is today
I'm looking for an algorithm that can check if nested regex repeats are reducible.
I have a check box list in that user can check or uncheck the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.