In C, an Odd or Even program determines whether a given integer is odd or even by checking the remainder when the number is divided by 2. If the remainder is zero, the number is even; otherwise, it is odd.

Steps for the Odd or Even Program:

  1. Include Header Files: Include the stdio.h library for input/output functions.
  2. Declare Variables: Use a variable to store the number that will be input by the user.
  3. Take Input: Ask the user to enter an integer.
  4. Check the Number: Use the modulus operator (%) to check if the number is divisible by 2.
  5. Display Output: Print whether the number is odd or even.

Program:

#include <stdio.h> int main() { int num; // Declare a variable to store the integer input by the user // Ask the user to enter an integer printf("Enter an integer: "); scanf("%d", &num); // Read the input from the user // Check if the number is even or odd using the modulus operator if (num % 2 == 0) { // If the remainder is 0, it is an even number printf("%d is even.\n", num); } else { // If the remainder is not 0, it is an odd number printf("%d is odd.\n", num); } return 0; }

Explanation:

  1. Variables:

    • int num: This variable stores the integer entered by the user.
  2. Input:

    • printf("Enter an integer: "); prompts the user to enter a number.
    • scanf("%d", &num); reads the input number from the user and stores it in the variable num.
  3. Checking Odd or Even:

    • The condition if (num % 2 == 0) checks whether the remainder is 0 when the number is divided by 2 using the modulus operator (%).
      • If num % 2 == 0, the number is even, and it prints the message num is even.
      • If num % 2 != 0, the number is odd, and it prints the message num is odd.
  4. Output:

    • Depending on the input and the condition, the program will display whether the number is even or odd.

Sample Output:

Example 1:

Enter an integer: 10 10 is even.

Example 2:

Enter an integer: 7 7 is odd.

Key Points:

  • The modulus operator (%) is crucial for checking divisibility. If the remainder is zero when dividing by 2, the number is even; otherwise, it's odd.
  • %d is the format specifier used to read and print integers.
  • Conditions: The if-else structure helps determine the result based on the input number.

This program provides a simple way to classify numbers as odd or even using basic input/output and conditional logic in C.