Write a C program to verify even or odd utilizing macro. How one can verify whether or not a given quantity is even or odd utilizing macro in C program. Logic to verify even or odd numbers utilizing macro.
In earlier put up 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 put up we’ll transfer a step additional. We are going to be taught so as to add situations to a macro.
On this put up you’ll come to discover ways to add fundamental situations to a macro. We are going to write a macro to verify if a given quantity is even quantity or not. In brief we’ll rework our even odd operate to macro.
Required data
Primary C programming, Macros, Bitwise operator
Throughout the course of C programming tutorials I’ve defined a number of methods to verify even or odd quantity. In case you missed any certainly one of them, under are some fast hyperlinks.
How one can discover even or odd utilizing macro?
Throughout the course of macro workout routines, we realized the right way to outline macro. So, allow us to get began and outline a macro which settle for a argument to verify for even or odd. Right here I’m utilizing bitwise operator to verify 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 should use the above macro to verify each even and odd.
Program to verify even or odd utilizing macro
/**
* C program to verify even or odd quantity utilizing macro
*/
#embrace <stdio.h>
// Outline macro to verify odd quantity
#outline IS_ODD(x) (x & 1)
int primary()
{
int num;
// Enter a quantity from person
printf("Enter any quantity to verify 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 verify even or odd: 22 22 is EVEN
Joyful coding 😉