Odd or Even Number Checker in C

This program determines if a given number is odd or even. Below is the C code for the program along with a detailed explanation of its structure and documentation.

Program Code


#include <stdio.h>

/**
 * Function to check if a number is odd or even.
 * @param number - the number to be checked
 * @return 1 if the number is odd, 0 if the number is even
 */
int isOddOrEven(int number) {
    return number % 2;
}

int main() {
    int number;

    // Prompt the user to enter a number
    printf("Enter a number: ");
    scanf("%d", &number);

    // Check if the number is odd or even
    if (isOddOrEven(number)) {
        printf("The number %d is odd.\n", number);
    } else {
        printf("The number %d is even.\n", number);
    }

    return 0;
}

Explanation

Here’s a step-by-step explanation of the program structure:

  1. Include Header File:
    #include <stdio.h>

    This header file is included to use the standard input-output functions like printf and scanf.

  2. Function Definition:
    
    /**
     * Function to check if a number is odd or even.
     * @param number - the number to be checked
     * @return 1 if the number is odd, 0 if the number is even
     */
    int isOddOrEven(int number) {
        return number % 2;
    }
                

    The isOddOrEven function takes an integer as input and returns 1 if the number is odd, and 0 if the number is even. It uses the modulus operator (%) to determine the remainder when the number is divided by 2.

  3. Main Function:
    int main() {
        int number;
    
        // Prompt the user to enter a number
        printf("Enter a number: ");
        scanf("%d", &number);
    
        // Check if the number is odd or even
        if (isOddOrEven(number)) {
            printf("The number %d is odd.\n", number);
        } else {
            printf("The number %d is even.\n", number);
        }
    
        return 0;
    }

    The main function is the entry point of the program.

    • It declares an integer variable number.
    • It prompts the user to enter a number using printf and reads the input using scanf.
    • It calls the isOddOrEven function to check if the number is odd or even and prints the result accordingly.
    • It returns 0 to indicate that the program ended successfully.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)