Even or Odd Number using Macros: C Program


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 Link: https://www.youtube.com/watch?v=QtiHjwNZ7zg [Watch the Video In Full Screen.]

Source Code: Even or Odd Number using Macros: C Program

  1. #include<stdio.h>  
  2. #define ODD_EVEN(num) ( (num % 2 == 0) ? printf("Even\n") : printf("ODD\n") )  
  3.   
  4. int main()  
  5. {  
  6.     int num;  
  7.   
  8.     printf("Enter a positive number\n");  
  9.     scanf("%d", &num);  
  10.   
  11.     ODD_EVEN(num);  
  12.   
  13.     return 0;  
  14. }  

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

Leave a Reply

Your email address will not be published. Required fields are marked *