First, let's create a new text file in your text editor and save it as 'evenOdd.c'. This file will contain our C code.
We'll start by including the standard input/output library with #include <stdio.h>
, which allows us to use functions like printf()
and scanf()
.
Then, we declare our main function where our code will execute. Inside, we'll declare an integer variable num
to store the user input.
We'll prompt the user to enter an integer with printf()
, and use scanf()
to read and store that input.
Using the modulus operator %
, we'll determine if num
is even or odd. If a number is divisible by 2 (i.e., num % 2 == 0
), it's even; otherwise, it's odd. We'll output the result using printf()
.
#include <stdio.h> int main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (num % 2 == 0) printf("%d is even.", num); else printf("%d is odd.", num); return 0; }
This program checks if the entered integer is even or odd.