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

In programming, one of the most basic tasks is to check whether a given number is odd or even. This is a common task in programming, especially in introductory programming courses. This article discusses an algorithm to check even and odd number in the C program.

Understanding Odd and Even Numbers

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 to Check Even and Odd Number

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.

Steps in the Algorithm to Check Even and Odd Number

Below are the steps in the algorithm to check even and odd number.

  • 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.

C Code of Algorithm to Check Even and Odd Number

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: C program to find odd or even 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 odd programs 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.

Conclusion
Now that you’ve been through the above programs, you should understand how to use C programming to determine if a given integer is odd or even. I hope you find our site to be useful and helpful. This concludes our discussion of the Odd & Even Program in C.

FAQs

Here are some frequently asked questions on this topic.

Q1: What is the purpose of the if-else statement in the program?
Ans: The if-else statement is used to output the result of the check, printing "Even" if the number is even and "Odd" if the number is odd.

Q2: Can the program check whether a decimal number is odd or even?
Ans: No, the program is designed to work only with integer input. It cannot check whether a decimal number is odd or even.

Q3: What happens if the user inputs a non-integer value?
Ans: The program will encounter an error and may crash. It is important to ensure that the input is of the correct data type.

Q4: Can the program check whether a string is odd or even?
Ans: No, the program cannot check whether a string is odd or even. It requires a numerical input.

Q5: Can the program check whether a large number is odd or even?
Ans: Yes, the program can check whether a large number is odd or even as long as it is within the range of the data type used (int in this case).

Q6: Can the program check whether a binary number is odd or even?
Ans: No, the program cannot check whether a binary number is odd or even. It requires a decimal input.

Q7: Can the program be modified to perform other operations on the input number?
Ans: Yes, the program can be modified to perform other operations on the input number by changing the logic in the if-else statement. However, it will no longer be checking whether the number is odd or even.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Selection Sort Program in C

Leave a Reply

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