C Program To Determine Leap Year or Not using Macros

In this video tutorial lets learn how to determine user input year is a leap year or not, using Macros and nested ternary / conditional operator.

Related Read:
C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator
Using Macro Template In Macro Expansion: C Program

Leap Year Logic

1. If a year is century year(year ending with 00) and is perfectly divisible by 400, then it’s a leap year.

2. If a year is not a century year, and is perfectly divisible by 4, then it’s a leap year.

Video Tutorial: Determine Leap Year or Not using Macros: C Program


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

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

Source Code: C Program To Determine Leap Year or Not using Macros

#include<stdio.h>

#define NOT_LEAP(x) printf("%d is not leap year\n", x)
#define LEAP_YEAR(x) printf("%d is leap year\n", x)

#define LEAP(x) ( (x % 100 == 0 && x % 400 == 0) ? LEAP_YEAR(x) : \
                 ( (x % 4 ==0) ? LEAP_YEAR(x) : NOT_LEAP(x)) )

int main()
{
    int year;

    printf("Enter a year\n");
    scanf("%d", &year);

    LEAP(year);

    return 0;
}

Output 1:
Enter a year
2018
2018 is not leap year

Output 2:
Enter a year
2019
2019 is not leap year

Output 3:
Enter a year
2020
2020 is leap year

Output 4:
Enter a year
2023
2023 is not leap year

Output 5:
Enter a year
2024
2024 is leap year

In above program we’re writing NOT_LEAP(x) macro to display a message that the user input year is not a leap year. LEAP_YEAR(x) macro is used to display message that the user input year is a leap year.

We use both NOT_LEAP(x) and LEAP_YEAR(x) macro names or macro templates inside LEAP(x) macro expansion.

We’re also using macro continuation(\) to break the line and continue writing the logic in next line in macro expansion of LEAP(x).

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

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

Biggest of 3 Numbers using Macros: C Program

In this video lets see how we can make use of Macros and ternary / conditional operator to find biggest of three numbers.

Related Read:
Biggest of 3 Numbers Using Ternary Operator: C
Preprocessors In C Programming Language

What we learn in this video tutorial?

We learn how to make use of nested ternary / conditional operator in macro definition. And how to use macro continuation( \ ) preprocessor operator.

Video Tutorial: Biggest of 3 Numbers using Macros: C Program


[youtube https://www.youtube.com/watch?v=CYWSkQ2-kp4]

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

Source Code: Biggest of 3 Numbers using Macros: C Program

#include<stdio.h>

#define BIGGEST(x, y, z) ( (x > y && x > z) ? x : ( y > z) ? y : z )

int main()
{
    int a, b, c;

    printf("Enter 3 integer numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    printf("Biggest of 3 numbers is %d\n", BIGGEST(a, b, c));

    return 0;
}

Output:
Enter 3 integer numbers
20
50
60
Biggest of 3 numbers is 60

Here we’re writing logic inside macro expansion. Wherever macro template is found in our source code, preprocessor replaces that macro template with macro expansion and the compiler compiles the code like normal source code.

Nested Ternary / Conditional Operator

Here we are using nested conditional operator. First we check if value of a is greater b and c, if true, value of a will be returned orelse we check if b is greater than c, if true, value of b will be returned orelse value of c will be returned.

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

C Program To Check Leap Year Using Ternary Operator

C Program to Find if a given Year is a Leap Year or Not, using Ternary Operator.

Related Read:
Ternary Operator / Conditional Operator In C
C Program To Check Leap Year

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.

We are illustrating the use of multiple nested ternary operator in this program. We have a nested ternary operator in place of expression2 as well as expression3.

Leap Year Logic

Leap Year
1. If a year is a century year(years ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Not Leap Year
1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year.
2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.

C Program To Check Leap Year Using Nested Ternary Operator


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

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


In this video tutorial we illustrate the use of multiple nested ternary operator / conditional operator inside a ternary operator.

First we check if given year is perfectly divisible by 100. If its true, then the entered year is a century year(year ending with 00). If the year is also perfectly divisible by 400, then its leap year, if not, its not a leap year.

If the user entered year is not a century year, then we check if the year is perfectly divisible by 4. If yes, then its a leap year, if not, then its not a leap year.

Check Leap Year Using Ternary Operator: C Program

 
#include < stdio.h >

int main()
{
    int year;

    printf("Enter the year\n");
    scanf("%d", &year);

    (year % 100 == 0) ?
    ( (year % 400 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    ) :
    ( (year % 4 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    );

    return 0;
}

Output 1:
Enter the year
2019
2019 is not leap year!

Output 2:
Enter the year
2004
2004 is leap year!

Output 3:
Enter the year
2024
2024 is leap year!

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

You can run the program and input any of the above leap year and check for the output.

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

Biggest of 3 Numbers Using Ternary Operator: C

Lets find biggest of 3 numbers using ternary operator / conditional operator. This program is an example for nested ternary operator.

Related Read:
Ternary Operator / Conditional Operator In C
Logical Operators In C

To find biggest of Three numbers using if-else control structure:
Biggest of 3 Numbers: C

Source Code: Biggest of Three Numbers using ternary operator: C

 
#include < stdio.h >

int main()
{
    int a, b, c, big;

    printf("Enter 3 numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    big = (a>b && a>c) ? (a) : ( (b>c)?(b):(c) );

    printf("Biggest is %d\n", big);

    return 0;
}

Output 1:
Enter 3 numbers
1
2
3
Biggest is 3

Output 2:
Enter 3 numbers
1
3
2
Biggest is 3

Output 1:
Enter 3 numbers
3
2
1
Biggest is 3

In above source code, if a is bigger than b as well as c, then value of a is returned and stored in variable big orelse exression3 gets executed – where we check if b is greater than c.

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.

Nested Ternary Operator

In our source code, we nest Ternary Operator inside expression3 to find biggest among b and c.

Note:
if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.

Biggest of 3 Numbers Using Ternary Operator: C Program


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

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


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