Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a program that asks the user to enter a number and then determines whether the number is even or odd.

C
#include <stdio.h>

int main() {
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

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

    return 0;
}

Explanation:

  • When you run this program, it will prompt the user to enter a number.
  • After the user inputs the number, the program will check if it is divisible by 2 (i.e., even) using the modulus operator (%).
  • If the number leaves no remainder when divided by 2, it is even; otherwise, it is odd.
  • The program will then display the appropriate message.
  • In this program, we declare a variable number of type int to hold the user input.
  • We use printf() to display the prompt message and scanf() to read the user input.
  • The if-else statement is used to check if the number is even or odd based on the remainder of the division by 2, and then we use printf() to display the result.

Output:

Output
Enter a number: 5
5 is an odd number.