C program to check odd or even and check any integer that divisible by another integer value
C program to check odd or even: In childhood As you have studied in mathematics that in decimal number system even numbers are divisible by 2 while odd are not divisible by 2, we may use modulus op...
Key Architecture Takeaways
- ›Point 1: Core computer science and engineering computing concept: C program to check odd or even and check any integer that divisible by another integer value.
- ›Point 2: Detailed trace of memory representation, algorithmic complexity, and control structures.
- ›Point 3: Includes portable ISO C (C17/C23) code examples verified with modern GCC and Clang.
Compiler & Toolchain Modernization
While legacy educational environments used 16-bit DOSBox Turbo C++, all code presented here adheres to standard ANSI/ISO C standards. You can compile these programs cleanly on modern Linux, Windows (MSYS2/WSL), or macOS using:
> gcc -Wall -Wextra -O2 program.c -o program
> ./program
> ```
---
C program to check odd or even: In childhood As you have studied in mathematics that in decimal number system even numbers are divisible by 2 while odd are not divisible by 2, we may use modulus operator(%) which returns remainder, For example, 7%2 gives 1 ( remainder when seven is divided by two). Even numbers are of the form 2*X and odd are of the form (2*X+1) where X is is an integer.We will determine whether a number is odd or even by using different methods all are provided with a code in c language.
<article id="node-323" class="node node-page node-full clearfix">
## C program to check odd or even using modulus operatorc
#include <stdio.h>
int main() { int a;
printf("Enter an integer\n"); scanf("%d", &a);
if (a%2 == 0) printf("Even\n"); else printf("Odd\n"); getch(); //This function holds your screen to see result. return 0; }
## Find odd or even using conditional operatorc
#include <stdio.h>
int main() { int n;
printf("Input an integer\n"); scanf("%d", &n);
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0; }
## C program to check odd or even without using bitwise or modulus operatorc
#include <stdio.h> int main() { int n; printf("Enter an integer\n"); scanf("%d", &n);
if ((n/2)*2 == n) printf("Even\n"); else printf("Odd\n");
return 0; }
In C programming language when we divide two integers we get an integer result, For example, the result of 10/3 will be 3. So we can take advantage of this and may use it to find whether the number is odd or even. Consider an integer n we can first divide by 2 and then multiply it by 2 if the result is the original number then the number is even otherwise the number is odd. For example 11/2 = 5, 5*2 = 10 (which is not equal to eleven), now consider 12/2 = 6 and 6*2 = 12 (same as original number). These are some logic which may help you in finding if a number is odd or not.
## **Find odd or even using conditional operator**c
#include<stdio.h>
main() { int a;
printf("Enter an integer\n"); scanf("%d",&a);
a%2 == 0 ? printf("Even number\n") : printf("Odd number\n"); return 0; }
We can use bitwise AND (&) operator to check odd or even, as an example consider binary of 7 (0111) when we perform 7 & 1 the result will be one and you may observe that the least significant bit of every odd number is 1, so ( odd_number & 1 ) will be one always and also ( even_number & 1 ) is zero.
## C program to check odd or even using bitwise operatorc
#include <stdio.h>
int main() { int a;
printf("Enter an integer\n"); scanf("%d", &a);
if (a & 1 == 1) printf("Odd\n"); else printf("Even\n");
return 0; }
### **To check the given no. is even or odd without using the % operator**c
#include <stdio.h>;
main() { char *a[]={"Odd", "Even"}; int n; printf("Enter an integer\n"); scanf("%d",&n);
printf("%s\n", a[(n/2)*2 == n]);
return 0; }
### **To check the given no. is divisible by 3 and also divisible by 5 (Using bitwise and operator) **c
#include<stdio.h> main() { int n; printf("Eneter any integer value \n"); scanf("%d",&n); if(n%3==0 && n%5==0) { printf("Entered integer is divisible by both 3 and 5"); } else { printf("Entered integer is not divisible by both 3 and 5"); } return 0; } ```
Output
odd even output
</article>
