Write a C program to test even or odd utilizing macro. Easy methods to test whether or not a given quantity is even or odd utilizing macro in C program. Logic to test even or odd numbers utilizing macro.
In earlier publish we realized so as to add fundamental logic to our macro. We realized to discover sq. and dice of an quantity utilizing macro. Right here on this publish we are going to transfer a step additional. We’ll be taught so as to add situations to a macro.
On this publish you’ll come to discover ways to add fundamental situations to a macro. We’ll write a macro to test if a given quantity is even quantity or not. In brief we are going to remodel our even odd perform to macro.
Required data
Fundamental C programming, Macros, Bitwise operator
Through the course of C programming tutorials I’ve defined a number of methods to test even or odd quantity. In case you missed any considered one of them, beneath are some fast hyperlinks.
Easy methods to discover even or odd utilizing macro?
Through the course of macro workouts, we realized find out how to outline macro. So, allow us to get began and outline a macro which settle for a argument to test for even or odd. Right here I’m utilizing bitwise operator to test even or odd quantity.
Instance:
#outline IS_ODD(x) (x & 1)
The above macro accepts an argument. It returns 1 if x
is odd in any other case returns 0. You need to use the above macro to test each even and odd.
Program to test even or odd utilizing macro
/**
* C program to test even or odd quantity utilizing macro
*/
#embody <stdio.h>
// Outline macro to test odd quantity
#outline IS_ODD(x) (x & 1)
int predominant()
{
int num;
// Enter a quantity from person
printf("Enter any quantity to test even or odd: ");
scanf("%d", &num);
if (IS_ODD(num))
printf("%d is ODDn", num);
else
printf("%d is EVENn", num);
return 0;
}
Enter any quantity to test even or odd: 22 22 is EVEN
Glad coding 😉