Write a program to check if a number is even or odd.

Check if a Number is Even or Odd in C

Understanding how to check if a number is even or odd is one of the fundamental concepts in programming. It serves as an introduction to conditionals, user input, and the basic structure of a C program. In this blog post, we will explore how to write a C program that checks whether a given number is even or odd. By the end, you’ll grasp not only how to implement this program but also the concepts behind it.

1. What Are Even and Odd Numbers?

Even numbers are integers that can be exactly divided by 2, meaning they have no remainder. Examples of even numbers include -4, -2, 0, 2, 4, etc. On the other hand, odd numbers are integers that cannot be evenly divided by 2, resulting in a remainder of 1. Examples of odd numbers are -3, -1, 1, 3, 5, etc. Understanding this distinction is crucial for implementing our program effectively.

2. Why Learn to Check for Even and Odd?

Checking if a number is even or odd is not just a simple exercise; it has practical applications in various programming scenarios:

  • Conditional Logic: This program introduces conditional statements, a core concept in programming that allows you to execute different code based on certain conditions.
  • Data Validation: Programs often need to validate user input, and checking for even or odd can be a part of this validation process.
  • Foundational Skills: This simple task serves as a building block for more complex programs, helping you become comfortable with the syntax and structure of C programming.

3. Writing the Program

Let’s dive into the code to create a program that checks if a number is even or odd. Below is the complete C program:

#include <stdio.h>

int main() {
    int number;

    // Prompt user for input
    printf("Enter an integer: ");
    scanf("%d", &number);

    // Check if the number is even or odd
    if (number % 2 == 0) {
        printf("%d is an even number.\n", number);
    } else {
        printf("%d is an odd number.\n", number);
    }

    return 0;
}
    

4. Explanation of the Code

Let’s break down the program line by line to understand how it works:

Line 1: #include <stdio.h>

This line includes the Standard Input Output library, which allows us to use functions like printf and scanf for outputting to the console and reading input from the user.

Line 2: int main() {

The main function is where the execution of the program begins. Every C program must have a main function.

Line 4: int number;

We declare a variable called number to store the integer input from the user.

Lines 6-8: Prompting User Input

Using printf, we ask the user to enter an integer. The scanf function then reads the input and stores it in the number variable.

Line 10: Checking Even or Odd

We use an if statement to check if the number is divisible by 2 (i.e., number % 2 == 0). If the condition is true, the program prints that the number is even; otherwise, it states that the number is odd.

Line 12: return 0;

This line indicates that the program has finished executing successfully.

5. Example Output

Here’s how the program looks when executed:

Enter an integer: 7
7 is an odd number.
    

Another example:

Enter an integer: 10
10 is an even number.
    

6. Compiling and Running the Program

To run the program, you need to compile it using a C compiler. Follow these steps:

  • Save the code in a file named even_odd.c.
  • Open your terminal or command prompt and navigate to the directory containing the file.
  • Compile the program using the following command:
  • gcc even_odd.c -o even_odd
  • Run the executable:
  • ./even_odd

7. Expanding the Program

Once you’re comfortable with this basic program, consider enhancing it with additional features:

  • Allow the user to check multiple numbers without restarting the program.
  • Implement error checking to ensure the user inputs an integer.
  • Extend the program to check for even or odd numbers in a range.

8. Conclusion

In this blog post, we learned how to write a C program to check if a number is even or odd. This exercise has provided insight into basic programming concepts such as conditionals, user input, and program structure. As you continue to practice and expand your programming skills, you'll find that these foundational concepts will support your growth as a developer. Keep experimenting, and happy coding!

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post