Check even or odd in C using if else ?


Q.) W.A.P to check the number is even or odd in C using if else ?
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
     int a;
     printf("enter a number :\n");
     scanf("%d",&a);
     if (a%2==0)
     {
        printf("a is an even number");
     }
	 else{
     printf( "a is an odd number");
	 }
     getch();
 }
Output
enter a number :
8    /* say a = 8 */.
a is an even number

Program Explanation


Step 1: Include header files (#include<stdio.h> and #include<conio.h>).

Step 2: Start with main function with return type.

Step 3: parenthesis to start and end the program { }.

Step 4: declare variables with data type i.e, 'a' is an integer type so we use "int" data type.

Step 5: Use output function printf() to print the output on the screen.

Step 6: Use input function scanf() to get input from the user.

Step 7: Here, we have to check whether the number is even or odd using if-else condition, Here, we use the arithmetic operator(i.e, %) as well as the assignment operator (i.e ==) to check condition. if condition checks whether 'a' is fully divisible by 2 or not.



  • if this condition is true, then it will execute if block, which will print the output as 'a' is an even number.
  • if the condition is not true then it will execute else block and print the output as 'a' is an odd number.
Step 8: using getch() function to hold the screen.