Understanding how to determine whether a number is odd or even forms one of the fundamental cornerstones of computer programming. It’s a straightforward task that is commonly used to introduce beginners to conditional statements, and it applies to almost any programming language.
In this article, we will focus on creating a C program to check if a number is odd or even. The logic behind determining if a number is even or odd is simple: an even number is always divisible by 2 without leaving any remainder, while an odd number, when divided by 2, always leaves a remainder.
C Program
Here is a simple program that accomplishes this task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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; } |
Code Explanation
Let’s break down the code to understand each part of it:
- `#include<stdio.h>`: This is a preprocessor directive that includes the standard input/output library in the program. This library allows us to use functions like `printf()` for output and `scanf()` for input.
- `int main()`: The `main`() function is the entry point of any C program. The execution of the program starts from this function. It must be present in every C program.
- `int num;`: Here, we declare an integer variable num to store the number that the user will input.
- `printf(“Enter an integer: “);`: This line prints a message asking the user to enter an integer.
- `scanf(“%d”, &num);`: This line reads the integer entered by the user and stores it in the num variable.
- `if(num % 2 == 0)`: This is a conditional statement that checks if the number entered by the user is even or odd. We use the modulo operator (%), which gives the remainder of a division operation. If the number is divisible by 2 with no remainder, the number is even, so `num % 2` will be equal to `0`.
- `printf(“%d is even.”, num);` and `printf(“%d is odd.”, num);`: These lines print the output to the console, telling the user whether the number is even or odd. If the if condition is true (i.e., the number is even), the first printf statement will execute. If the if condition is false (i.e., the number is odd), the program will execute the else block.
- `return 0;`: This line signifies the successful termination of the `main()` function and the end of the program.
Exception Handling
Let’s modify our earlier example to include error handling, using errno and `strerror()`. The `strerror()` function, declared in `string.h`, returns a string that describes the error code passed in the argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h> #include<errno.h> #include<string.h> int main() { int num; errno = 0; // ensure errno is reset before we use it printf("Enter an integer: "); int result = scanf("%d", &num); // Check if scanf function was successful if(result == EOF) { // If not, print the error message printf("An error occurred! %s\n", strerror(errno)); return 1; // Return error status } // If scanf function was successful, check if the number is odd or even if(num % 2 == 0) printf("%d is even.\n", num); else printf("%d is odd.\n", num); return 0; } |
In this program, `scanf()` returns EOF (End Of File) if it encounters an input failure. We can then check errno to identify the specific error that occurred. If an error does occur, we use `strerror()` to print a human-readable string describing the error, and then return an error status code from `main()`.
Conclusion
The ability to determine whether a number is odd or even is a simple yet vital task in programming, as it’s a building block for understanding more complex logical operations. This program can be easily extended or modified to perform more complex tasks, making it a good stepping stone for those new to the world of programming.