Algorithm
Odd or Even number algorithm is very simple.
In mathematics, to check if any number is odd or even - we could divide it 2, and if got any reminder, then it is odd number, for even number, reminder would be 0.
So, here's algorithm for odd and even number in programming -
- Number % 2 == 0
- Even Number
- Number % 2 != 0
- Odd Number
Code Examples
#1 Odd and Even Number Check in C with input
Code -
C Programming
#include <stdio.h>
int main()
{
int number;
printf("Enter a number to check odd or even: ");
scanf("%d", &number);
if (number %2 == 0){
printf("Even Number");
}else{
printf("Odd Number");
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
Even Number
#2 Odd and Even Number List Generate C Programming
Code -
C Programming
#include <stdio.h>
int main(void){
int number, i;
printf("Enter upto number to generate odd and even number: ");
scanf("%d", &number);
printf("\nEven Number List: ");
for(i = 0; i < = number; i++){
if(i % 2 == 0){
printf("%d ", i);
}
}
printf("\nOdd Number List: ");
for(i = 0; i <= number; i++){
if(i % 2 != 0){
printf("%d ", i);
}
}
printf("\n">;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
Even Number List: 0 2 4 6 8 10 12 14 16 18 20
Odd Number List: 1 3 5 7 9 11 13 15 17 19
Demonstration
Odd and Even Number is the basic arithmatic reminder operation.
- If number is divided by 2 without any reminder - It's an Even Number
- If number is not divided by 2 without any reminder - It's an Odd Number