Even Odd in C

Published on 05 October 2020 (Updated: )

Even Odd in C

In this article, we will check whether an integer is even or odd using C.

How to Implement the Solution

Let’s take a look on code for this program in C.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{    
    if (argc == 1 || argv[1][0] == '\0' || (atoi(argv[1]) == 0 && strcmp(argv[1], "0") != 0)) {
        printf("Usage: please input a number\n");
    } else {
        int input = atoi(argv[1]);
        if (input % 2 == 0) {
            printf("Even\n");
        }
        else {
            printf("Odd\n");
        }
    }

    return 0;
}

Let’s understand this code block by block.

Main Function

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {

In the first three lines, we are including header files using include directive to utilise some functions defined in header files later in the program. Here, Standard Input/Output header file(<stdio.h>) is called to use printf() function, C Standard Library(<stdlib.h>) to use atoi() function and <string.h> to use strcmp() function. 

In C, we declare a function using general form:

return_type function_name(parameter){
  ...
}

So, we are declaring main function with return_type integer and argc and argv as parameters to access command line arguments. argc and argv are variables which main function will get when run in command-line. argc stores argument count while argv stores array of strings that are arguments. This should be kept in mind that all command-line arguments are stored as strings.

argv[0] represents first argument which always is equal to name of our program. If we type the following command in terminal:

./even-odd 2

Here, ./even-odd represents argv[0] and 2 represents argv[1].

Control Flow

Before we move onto the control flow, let’s look on the functions which we called from header files. atoi() converts argument string into an integer. strcmp() compares two strings and printf() prints formatted string as output.
For the if-else statements, first if condition checks if the argument count is 1 or if the argument provided is only a null string or if the argument is equal to 0(integer). For that it prints correct usage pattern.
Otherwise the value of argv[1] is converted into integer and stored in variable input. % represents modulo operator which gives remainder for integer division. So, in case of input % 2, 0 as remainder shows that input is divisible by 2 and non zero remainder shows indivisibility. Thus, prints Even or Odd depending on input for the next if statement.
Return value is called exit code. So, 0 as exit code represents zero error.

How to Run the Solution

To run a C program, we need to install GCC compiler and run the following command in terminal.

gcc -o even-odd even-odd.c
./even-odd

Another handy option is to compile and run using online C Compiler such as OnlineGDB, Repl

Further Reading