Lets write a C program to find if the user input number is odd or even, using Macros.
Related Read:
Even or Odd Number using Ternary Operator: C Program
Logic To Find Even Or ODD
If user input number is perfectly divisible by 2, then it’s Even number orelse it’s Odd number.
Video Tutorial: Even or Odd Number using Macros: C Program
[youtube https://www.youtube.com/watch?v=QtiHjwNZ7zg]
Source Code: Even or Odd Number using Macros: C Program
#include<stdio.h> #define ODD_EVEN(num) ( (num % 2 == 0) ? printf("Even\n") : printf("ODD\n") ) int main() { int num; printf("Enter a positive number\n"); scanf("%d", &num); ODD_EVEN(num); return 0; }
Output 1:
Enter a positive number
5
ODD
Output 2:
Enter a positive number
6
Even
In above program the macro template ODD_EVEN(num) will be replaced by it’s macro expansion ( (num % 2 == 0) ? printf(“Even\n”) : printf(“ODD\n”) ) and hence if the user input number is perfectly divisible by 2, then EVEN will be printed else ODD will be printed.
Related Read
Ternary Operator / Conditional Operator In C
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert