In this tutorial, we are looking new program which is to find an even or odd number.
In this program, we first have a number and we check whether that number is Even or odd programmatically.
Definition of odd and even numbers
What is even number?
Any number which is divided by 2 and gives the remainder zero is Even number.
What is an odd number?
Any number which is divided by 2 but gives reminder 1 is an odd number.
To check whether a number is even or odd we need some basic mathematical formula.
The mathematical formula to find out whether a number is even or odd is as follows
Result = Number % 2
In above formula, The number is any number of which we are checking even or odd number.
If this number gives the reminder 0 then it’s even number else number is odd.
Example
Number = 10
Reminder = Number % 2
Reminder = 0
So, Number is even
Number = 5
Reminder = Number % 2
Reminder = 1
So, Number is odd.
In this way, we check whether Number is even or odd.
Using this way we implement the program to find even or odd in c.
C Program to Check Whether the Given Number is Even or Odd
12345678910111213141516171819202122232425262728293031 | #include<stdio.h>void main(){ int number; printf(“Enter a Number \n”); scanf(“%d”, &number); if(number % 2 == 0) { printf(“The number %d is Even”, number); } else { printf(“The number %d is Odd”,number); } printf(“\n”);} |
Output
Enter a Number
5
The number 5 is Odd
Explanation
- First, Here we declared an integer number.
- Then we took the input from a user using scanf statement.
- As we know modulus operator is used to return the remainder of two values as (10%3=1, 12%2=0).
- So if you do modulus with 2 then the remainder would be ‘zero’ for Even numbers and ‘1’ for Odd numbers.
- If we divide ‘number’ with ‘2’ using mod operator then depending upon condition the respective ‘printf’ will be executed.
- In above output, 5 % 2 = 1 so The number is odd will be printed.
Other C language solved programs