- 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.
- Here is the example program for how to find even of odd number without using modulus in c programming language
Program #1: write a c program to check a number is even or odd without using modulus and division operators in c programming.
- Even or odd program in c without using mod operator
- #include <stdio.h>
- 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) <<1==number then it is even number
- Like this we can find even or odd number without using modulus in c
Program #2: write a c program to check odd or even without using modulus operator
- #include <stdio.h>
- int main()
- {
- int number;
- printf("Enter a number to check even or odd");
- scanf("%d", &number);
- if(( number >> 1) <<1==number)
- 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
logic batao bhai line no 9 prog. 1st
ReplyDelete