C Program to check if a number is even or odd in a given interval

Category: C Program

Learn how to write a C program to check if numbers in a given interval are even or odd using functions. This article provides a complete code example and explanation, highlighting the benefits of using functions for modular programming.

Determining whether a number is even or odd is a basic concept in programming and mathematics. In this article, we will explore how to write a C program that checks if each number in a given interval is even or odd. We'll implement this functionality using a function, which makes the code more modular and reusable. We'll discuss the problem, explain the solution, and provide a complete implementation with appropriate explanations.

Understanding Even and Odd Numbers

A number is considered even if it is divisible by 2 without a remainder, and odd if it leaves a remainder of 1 when divided by 2. The mathematical representation for checking even or odd numbers is:

  • Even: number % 2 == 0
  • Odd: number % 2 != 0

Write a C Program to check if a number is even or odd in a given interval

We will create a C program that includes a function checkEvenOdd to determine whether a number is even or odd. This function will be called for each number in the specified interval.

#include <stdio.h>

// Function to check if a number is even or odd
void checkEvenOdd(int num) {
    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }
}

int main() {
    int start, end;

    // Input the start and end of the interval from the user
    printf("Enter the start of the interval: ");
    scanf("%d", &start);
    printf("Enter the end of the interval: ");
    scanf("%d", &end);

    // Check each number in the interval for even or odd
    printf("Checking numbers in the interval [%d, %d]:", start, end);
    for (int i = start; i <= end; i++) {
        checkEvenOdd(i);
    }

    return 0;
}

Output

Enter the start of the interval: 3
Enter the end of the interval: 20
Checking numbers in the interval [3, 20]:3 is odd.
4 is even.
5 is odd.
6 is even.
7 is odd.
8 is even.
9 is odd.
10 is even.
11 is odd.
12 is even.
13 is odd.
14 is even.
15 is odd.
16 is even.
17 is odd.
18 is even.
19 is odd.
20 is even.

Explanation of the Code

  1. Function checkEvenOdd:
    • This function takes an integer num as input.
    • It checks if the number is even or odd using the modulus operator %.
    • If num % 2 == 0, it prints that the number is even; otherwise, it prints that the number is odd.
  2. Main Function main:
    • The program starts by declaring two integers, start and end, which represent the bounds of the interval.
    • The user is prompted to enter the start and end of the interval.
    • A for loop iterates through each number in the interval, calling the checkEvenOdd function for each number.
    • The results are printed to the console, indicating whether each number is even or odd.

Recommended Posts