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 https://www.youtube.com/watch?v=QtiHjwNZ7zg]

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

#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

Even or Odd Number using Ternary Operator: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, using Ternary / Conditional Operator.

Related Read:
Basic Arithmetic Operations In C
Relational Operators In C

Ternary Operator / Conditional Operator In C

Even or Odd Number: C Program

Even or Odd Number without using Modular Division: C Program

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

C program To check Even or Odd Number using Ternary Operator

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter an integer number\n");
    scanf("%d", &n);

    (n % 2 == 0) ?
    (printf("%d is Even number\n", n)) :
    (printf("%d is Odd  number\n", n));

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Even or Odd Number using Ternary Operator: C Program


[youtube https://www.youtube.com/watch?v=TMT53s8bhzQ]

YouTube Link: https://www.youtube.com/watch?v=TMT53s8bhzQ [Watch the Video In Full Screen.]


Modular division returns remainder of division. For example, 12 / 2 = 6. But 12 % 2 = 0.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is perfectly divisible by 2. If its perfectly divisible by 2, then it’e even number or else its odd number.

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

Even or Odd Number: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD.

Related Read:
Basic Arithmetic Operations In C
Relational Operators In C

Even or Odd Number without using Modular Division: C Program
Even or Odd Number using Ternary Operator: C Program

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

C program To check Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter an integer number\n");
    scanf("%d", &n);

    if(n % 2 == 0)
    {
        printf("%d is even number\n", n);
    }
    else
    {
        printf("%d is odd number\n", n);
    }

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

C program To check Even or Odd Number


[youtube https://www.youtube.com/watch?v=CAgJxUh8bEg]

YouTube Link: https://www.youtube.com/watch?v=CAgJxUh8bEg [Watch the Video In Full Screen.]


Modular division returns remainder of division. For example, 12 / 2 = 6. But 12 % 2 = 0.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is perfectly divisible by 2. If its perfectly divisible by 2, then it’e even number or else its odd number.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert