- If a number is divisible by 2 then is it an even number otherwise it is odd number.
- Now we need to check a number is even or odd without using modulus or division operators in c programming language.
- Yes we can check a number is even or odd without using division and modulus operators for that we need to use & operator
- number &1 ==1 then it is an even number.
- Let us check a number is even or not in c program using & operator.
Program #1: write a c program to check a Number is even or odd without using modulus and division operators in c programming.
- #include
- int main()
- {
- int number;
- printf("Enter a number to check even or odd");
- scanf("%d", &number);
- if((number & 1)==0)
- printf("%d is even.", number);
- else
- printf("%d is odd.", number);
- getch();
- }
Output:
- We can check a number is even or odd without using modulus and division operator in c program
- The another method is using shift operators
- number >> 1)
Program #2: write a c program to check odd or even without using modulus operator
- #include
- int main()
- {
- int number;
- printf("Enter a number to check even or odd");
- scanf("%d", &number);
- if(( number >> 1)
- printf("%d is even.", number);
- else
- printf("%d is odd.", number);
- getch();
- }
Output:
- Enter a number to check even or odd
- 4
- 4 is Even Number