by Dinesh Thakur Category: Control Structures

This if-else statement prints whether an integer number n is odd or even. A number is even if it is divisible by 2. Thus, if expression n % 2 == 0 is true, number n is even; otherwise it is odd. Alternatively, we can write this if statement as follows:

 if (n % 2 == 1)

printf ("odd");

else printf("even");

Note that as the arithmetic expression n % 2 evaluates as 1 (i. e., true) when n is odd and 0 (i. e., false); otherwise, the equality test in this if-else statement is not required. Thus, we can write the test expression in simply as n % 2. However, such statements are somewhat difficult to understand and should be avoided, particularly by beginners.

void main()
            int n ;
           clrscr( ) ;
           cout<<"Enter any number : ";
           cin>>n ;
            if (n%2 == 0)
                 cout<<"The number is even"<<endl ;
                 cout<<"The number is odd" ;
                 cout<<"Press any Key to Exit.";
             getch() ;

Find out Whether It is An Odd Number Or Even Number

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles