Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C Program to Check the Given Number is Odd or Even

Last Updated on July 12, 2023 by Mayank Dham

Odd numbers are the numbers that are not divisible by two i.e. 1, 3, 5, 7, and so on, whereas even numbers are the numbers that are divisible by two i.e. 0, 2, 4, 6 and so on. Let’s figure out in every possible way to find even or odd program in C. We will discuss different ways and methods to implement odd or even program in C, also we will look into flowchart to find whether a number is odd or even, we will see different algorithm for even or odd numbers in C programming.This article discusses an algorithm to check even and odd number in the C program.

What is an Even or Odd Program in C Programming?

Odd and even numbers are types of integers, which are whole numbers (positive, negative, or zero) that are not fractions or decimals. An even number is any integer that is divisible by 2, meaning it has no remainder when divided by 2. For example, 2, 4, 6, 8, and 10 are all even numbers. On the other hand, an odd number is any integer that is not divisible by 2, meaning it has a remainder of 1 when divided by 2. For example, 1, 3, 5, 7, and 9 are all odd numbers.

Explanation of Algorithm for Even or Odd Numbers in C

The goal of the odd-even program is to determine whether a given number is odd or even. To do this, the program first prompts the user to enter an integer using the printf function and reads the input using the scanf function. The entered number is then stored in an integer variable.

Next, the algorithm determines whether the entered number is odd or even, and after figuring it out, the algorithm displays the output and stops working.

Algorithm for Even or Odd numbers in C Programming

Below are the steps in the algorithm for even or odd numbers in C programming.

  • Step 1– Start the program.
  • Step 2– Read/input the number.
  • Step 3– Check if the number is even or odd.
  • Step 4– display the output.
  • Step 5– Stop the program.

Code of Algorithm for Even or Odd Numbers in C

There are several algorithm to check even and odd number in C.

Method 1: Odd-even program in C using the modulus operator
Below is the code and explanation of the algorithm to check even and odd number using the modulus operator in C.

#include <stdio.h>
int main()
{
  int n;
  printf("Enter an integer\n");
  scanf("%d", &n);
  if (n%2 == 0)
    printf("Even\n");
  else
    printf("Odd\n");
  return 0;
}

Explanation: In the above code, the algorithm uses the modulus operator % to check if the value of n is divisible by 2. If the remainder of the division operation is 0, then n is even, and the program will execute the code in the first if statement. If the remainder is 1, then n is odd, and the program will execute the code in the else statement.

Method 2: Even or odd program in C using a bitwise operator
Below is the code and explanation of the algorithm to check even and odd number using the bitwise operator in C.

#include <stdio.h>
int main()
{
  int n;   
  printf("Input an integer\n");
  scanf("%d", &n);
  if (n & 1 == 1)
    printf("Odd\n");
  else
    printf("Even\n");   
  return 0;
}

Explanation: In the above code, the program uses the bitwise AND operator ( & ) to perform a bit-level check on the value of n. Specifically, the program is checking whether the least significant bit (i.e., the rightmost bit) of n is 1. If this bit is 1, then n is odd. If this bit is 0, then n is even.

Method 3: Even or Odd Program in C using conditional operator
Below is the code and explanation of the algorithm to check even and odd number using the conditional operator in C.

#include <stdio.h>
int main()
{
  int n;
  printf("Input an integer\n");
  scanf("%d", &n);
  n%2 == 0 ? printf("Even\n") : printf("Odd\n");
  return 0;
}

Explanation: In the above code, the program uses the ternary operator ‘? :’ to check whether the input number is even or odd. It is a shorthand way of writing an if-else statement. If the remainder of the input number divided by 2 is 0, it is an even number, so the printf() function is used to display "Even" on the screen. If the remainder is not 0, it is an odd number, so the printf() function is used to display "Odd" on the screen.

Method 4: Even or Odd Program in C without Using Bitwise & Modulus Operator
Till now we use Bitwise and Modulus Operators. But in this program, we will not use these operators to implement even or odd program in C programming.

Code Implementation

#include <stdio.h>
int main()
{
  int n = 11;
  if ((n/2)*2 == n)
    printf("the number is Even\n");
  else
    printf("the number is Odd\n");
return 0;
}

Output

the number is Odd

Conclusion
In conclusion, the Even or Odd program in C serves as a fundamental example of applying basic programming concepts to determine whether a given number is even or odd. By leveraging the modulus operator (%), we can easily check the remainder when dividing the number by 2. If the remainder is 0, the number is even; otherwise, it is odd.

Throughout this article, we explored the syntax and logic behind the "Even or Odd" program in C. We learned how to prompt the user for input, store the input in a variable, and apply conditional statements (if-else) to perform the even/odd check. The program’s simplicity makes it an excellent starting point for beginners learning C or programming concepts in general.

FAQs (Frequently Asked Questions) – Even or Odd Program in C:

Here are some frequently asked questions odd or even program in C programming.
Q1. What does the modulus operator (%) do in the "Even or Odd" program?
The modulus operator (%) returns the remainder when one number is divided by another. In the program, we use the modulus operator to check the remainder when dividing the input number by 2. If the remainder is 0, the number is even; otherwise, it is odd.

Q2. How can I handle decimal or floating-point numbers in the program?
The "Even or Odd" program is designed to work with integers. If you input a decimal or floating-point number, the program will consider only the integer part and ignore the decimal portion. For precise handling of floating-point numbers, you may need to modify the program or use specific techniques to convert the input.

Q3. Can the program handle negative numbers?
Yes, the program can handle negative numbers. The modulus operator works with negative numbers in C. If you input a negative number, the program will still determine whether it is even or odd based on the mathematical rules of integer division and modulus operation.

Q4. What happens if I input a non-numeric value or a character?
If you input a non-numeric value or a character, the program will not be able to recognize it as a valid input. It may produce unexpected results or encounter runtime errors. It is important to validate and sanitize user inputs to ensure the program’s correctness and prevent any unintended consequences.

Q5. Can I modify the program to handle multiple numbers or a series of inputs?
Absolutely! The provided program demonstrates the concept of checking even or odd for a single input number. If you want to extend it to handle multiple numbers or a series of inputs, you can use loops (such as for or while loops) to repeatedly prompt the user for input and perform the even/odd check.

Q6. How can I modify the program to display additional information, such as the factors of the number?
If you want to enhance the program to display additional information, like the factors of the number, you will need to modify the code accordingly. You can introduce additional logic and computations to determine and display the desired information alongside the even/odd check.

Leave a Reply

Your email address will not be published. Required fields are marked *