Home » C programs

Check EVEN or ODD using Macro in C language

In this post, we are going to implement a C program, that will read an integer number check whether it is EVEN or not by using Macros.
Submitted by IncludeHelp, on April 22, 2018

There are many ways to check whether a given number is EVEN or ODD, which we have already wrote in other posts; here you can also read them

There is another way, which we are going to implement here...

We will check whether a given number is EVEN or ODD by using Macro (Read more: Macro in C). In the Macro definition, we will use condition operator (ternary operator) to validate the conditions.

C program to check EVEN or ODD using Macros in C

Macro definitions:

    #define ISEVEN(n)   ((n%2 == 0) ? 1 : 0)
    #define ISODD(n)    ((n%2 != 0) ? 1 : 0)

Program:



#include <stdio.h>

#define ISEVEN(n)	((n%2 == 0) ? 1 : 0)
#define ISODD(n)	((n%2 != 0) ? 1 : 0)

int main(void) 
{
	
	int number;
	
	printf("Enter an integer number: ");
	scanf("%d",&number);
	
	if(ISEVEN(number))
	    printf("%d is an EVEN number\n",number);
	else if(ISODD(number))
	    printf("%d is an ODD number\n",number);
	else 
	    printf("An Invalid Input\n");
		
	return 0;
}

Output

    First run:
    Enter an integer number: 100
    100 is an EVEN number

    Second run:
    Enter an integer number: 101
    101 is an ODD number

We can also use One Macro to validate both (EVEN or ODD)



if(ISEVEN(number))
	printf("%d is an EVEN number\n",number);
else
	printf("%d is an ODD number\n",number);




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.