Check Even or Odd number program in C
Program Description:
Write a C Program to check if the given number is an Even number or Odd number ( an Even odd program ).
Example 1:
Input:
1 |
44 |
Output:
1 |
44 is Even Number |
Example 2:
Input:
1 |
7 |
Output:
1 |
7 is an Odd Number |
Even or Odd Number Program Logic:
Any Number that can be divided exactly by 2 is an Even number. Even numbers give zero as the remainder when divided by 2.
Even Number Examples: 2, 4, 50, 100, etc.
Similarly, any number which is not exactly divided by 2 is called Odd number. Odd numbers give 1 as the remainder when divided by 2.
Odd Number Examples: 5, 31, 59, 99, etc.
We are going to use the above logic that Even numbers give us the zero remainder and Odd number gives one (1) as remainder when divided by 2.
We can get the remainder of any integer using the Modulus operator (%).
If the Modulus of input number is zero, Then the Input number is Even number.
If the Modulus of input number is One(1), Then the Input number is Odd number.
Let’s convert above logic into the code.
Program to check Even or Odd Number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main() { int n; // Take the input from the user printf("Enter any number : "); scanf("%d",&n); // Calculate the Remainder using the modulus operator if(n%2 == 0) { // Remainder is zero. Given number is Even. printf("%d is Even Number \n", n); } else { // Remainder is non-zero. Given number is Odd. printf("%d is an Odd Number \n", n); } return 0; } |
Program Output:
We are using gcc compiler in Linux OS.
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 27 28 |
// Compilling using gcc compiler venkey@venkey$ gcc even-odd.c // Run the executable 'a.out' venkey@venkey$ ./a.out Enter any number : 4 4 is Even Number venkey@venkey$ ./a.out Enter any number : 100 100 is Even Number venkey@venkey$ ./a.out Enter any number : 5 5 is an Odd Number venkey@venkey$ ./a.out Enter any number : -5 -5 is an Odd Number venkey@venkey$ ./a.out Enter any number : -77 -77 is an Odd Number venkey@venkey$ ./a.out Enter any number : -10 -10 is Even Number venkey@venkey$ |
As you can see from above output, We are getting the expected output. Please play around with program by providing the different inputs.
Let’s write another program to display all Even numbers and Odd numbers from 0 to 100 using loops in C language.
Example 1: Program to display all Even Numbers and Odd numbers from 0 to 100.
We are going to use the same logic as above. i.e To use the modulus operator to calculate the remainder. But instead of checking for one value, We are going to print all even and odd numbers between 0 and 100.
We are going to use the for loop to iterate over numbers.
Even Odd Number ( 0 to 100) Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<stdio.h> int main() { int count = 0; // Start the loop from 0 // Continue until we reach 100 // At each iteration, Calculate the number is even or odd for(count=0; count <= 100; count++) { // Calculate the Remainder using the modulus operator if(count%2 == 0) { // Remainder is zero. Given number is Even. printf("%d is Even Number \n", count); } else { // Remainder is non-zero. Given number is Odd. printf("%d is an Odd Number \n", count); } } return 0; } |
Output:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
venkey@venkey$ gcc zeroTo100.c venkey@venkey$ ./a.out 0 is Even Number 1 is an Odd Number 2 is Even Number 3 is an Odd Number 4 is Even Number 5 is an Odd Number 6 is Even Number 7 is an Odd Number 8 is Even Number 9 is an Odd Number 10 is Even Number 11 is an Odd Number 12 is Even Number 13 is an Odd Number 14 is Even Number 15 is an Odd Number 16 is Even Number 17 is an Odd Number 18 is Even Number 19 is an Odd Number 20 is Even Number 21 is an Odd Number 22 is Even Number 23 is an Odd Number 24 is Even Number 25 is an Odd Number 26 is Even Number 27 is an Odd Number 28 is Even Number 29 is an Odd Number 30 is Even Number 31 is an Odd Number 32 is Even Number 33 is an Odd Number 34 is Even Number 35 is an Odd Number 36 is Even Number 37 is an Odd Number 38 is Even Number 39 is an Odd Number 40 is Even Number 41 is an Odd Number 42 is Even Number 43 is an Odd Number 44 is Even Number 45 is an Odd Number 46 is Even Number 47 is an Odd Number 48 is Even Number 49 is an Odd Number 50 is Even Number 51 is an Odd Number 52 is Even Number 53 is an Odd Number 54 is Even Number 55 is an Odd Number 56 is Even Number 57 is an Odd Number 58 is Even Number 59 is an Odd Number 60 is Even Number 61 is an Odd Number 62 is Even Number 63 is an Odd Number 64 is Even Number 65 is an Odd Number 66 is Even Number 67 is an Odd Number 68 is Even Number 69 is an Odd Number 70 is Even Number 71 is an Odd Number 72 is Even Number 73 is an Odd Number 74 is Even Number 75 is an Odd Number 76 is Even Number 77 is an Odd Number 78 is Even Number 79 is an Odd Number 80 is Even Number 81 is an Odd Number 82 is Even Number 83 is an Odd Number 84 is Even Number 85 is an Odd Number 86 is Even Number 87 is an Odd Number 88 is Even Number 89 is an Odd Number 90 is Even Number 91 is an Odd Number 92 is Even Number 93 is an Odd Number 94 is Even Number 95 is an Odd Number 96 is Even Number 97 is an Odd Number 98 is Even Number 99 is an Odd Number 100 is Even Number venkey@venkey$ |
Related Math Programs:
- Arithmetic Operations in C programming – SillyCodes
- Armstrong Number Program in C Language – SillyCodes
- C Program to find sum of digits of a Number | Sum of Digits program in C – SillyCodes
- C program to print all ASCII characters and ASCII values/numbers – SillyCodes