This program checks a number to see if it is even or odd. The amount that the user types in is saved in the number variable. The modulus operator (%) is used in the if statement to check if the number is divided by 2. If there is no leftover, the number is even. If there is one, it is odd. This program teaches you how to use if-else sentences to make simple decisions.

#include <stdio.h>

int main() {
    int number;
    printf("Enter a number: ");  // Ask the user for a number
    scanf("%d", &number);  // Read and store the number
    if (number % 2 == 0)  // Check if the number is divisible by 2
        printf("%d is even.\n", number);  // If true, print it's even
    else
        printf("%d is odd.\n", number);  // If false, print it's odd
    return 0;
}

Leave a Reply

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

Verified by MonsterInsights