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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.