#include <stdio.h>

int main() 
{
    // Declare an integer variable to hold the user input
    int number;

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

    // Check if the number is even or odd
    if (number % 2 == 0) {
        // If the remainder when divided by 2 is 0, it's even
        printf("%d is an even number.\n", number);
    } else {
        // If the remainder is not 0, it's odd
        printf("%d is an odd number.\n", number);
    }

    return 0; // End of the program
}

Explanation:

  1. Including the standard input-output library: We include the stdio.h header file, which allows us to use functions like printf and scanf for input and output.cCopy code#include <stdio.h>
  2. Main Function: Every C program starts execution from the main() function. Inside this function, we’ll write our code logic.cCopy codeint main() { ... }
  3. Declaring a variable: We declare an integer variable number to store the number that the user will input.cCopy codeint number;
  4. User Input: We prompt the user to enter a number using the printf function. Then, we use scanf to capture the input and store it in the variable number.cCopy codeprintf("Enter an integer: "); scanf("%d", &number);
  5. Checking if the number is even or odd:
    • We use the modulus operator (%) to check whether the remainder of the number divided by 2 is zero.
    • If the remainder is 0, it means the number is even, so the program prints that the number is even.
    • If the remainder is not 0, the number is odd, and the program prints that the number is odd.
    cCopy codeif (number % 2 == 0) { printf("%d is even.\n", number); } else { printf("%d is odd.\n", number); }
  6. Returning 0: Finally, the program returns 0 to indicate that it executed successfully.cCopy codereturn 0;

OUTPUT

Enter an integer: 8
8 is even.

Summary:

  • The % operator helps us check if a number is divisible by 2.
  • The program handles both even and odd numbers by displaying a message accordingly.

Leave a Reply

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

Verified by MonsterInsights