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

C Program To Check Leap Year or Not using Function

Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.

Related Read:
C Program To Check Leap Year

Video Tutorial: C Program To Check Leap Year or Not using Function


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

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

Source Code: C Program To Check Leap Year or Not using Function

#include<stdio.h>
#include<stdbool.h>

bool leap(int); // function prototype

int main()
{
    int year;

    printf("Enter a year to find leap year or not\n");
    scanf("%d", &year);

    //function call leap(year);
    if( leap(year) )
    {
        printf("%d is leap year\n", year);
    }
    else
    {
        printf("%d is not leap year\n", year);
    }

    return 0;
}

//function definition
bool leap(int year)
{
    if(year % 100 == 0)
    {
        if(year % 400 == 0)
            return true;
        else
            return false;
    }
    else
    {
        if(year % 4 == 0)
            return true;
        else
            return false;
    }
}

Output 1
Enter a year to find leap year or not
2020
2020 is leap year

Output 2
Enter a year to find leap year or not
2021
2021 is not leap year

Logic To Find Leap Year or Not: Using Function

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

If the user entered year is not a century year and the year is perfectly divisible by 4, then its a leap year orelse it’s not a leap year.

In our function, we return true if the user entered year is leap year and return false if the user entered year is not leap year.

Note: User defined function leap has a return type of bool i.e., it returns either true or false. For our C program to support bool type we are including stdbool.h header file.

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 Determine Leap Year or Not using Logical Operators

Any year is entered through the keyboard, write a C program to determine whether the year is a leap year or not. Use the logical operators && and ||.

Related Read:
if else statement in C
Relational Operators In C
Logical Operators In C

Other Leap Year C Programs

C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator

Leap Year Logic

1. If a year is a century year(year 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.

Century year is determined by modulo division of the entered year by 100. Example: year%100 == 0. If its century year, it must also be perfectly divisible by 400. i.e., year%400 == 0
(year%100 == 0 && year%400 == 0)

If the year is not century year, then it must be perfectly divisible by 4, for the year to be a leap year.
(year%100 != 0 && year%4 == 0)

Expected Output for the Input

User Input:
Enter a year
2024

Output:
2024 is a leap year

Video Tutorial: C Program To Determine Leap Year or Not using Logical Operators


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

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

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

#include < stdio.h >

int main()
{
    int year;

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

    if( (year%100 == 0 && year%400 == 0) ||
        (year%100 != 0 && year%4   == 0)   )
    {
        printf("%d is a leap year\n", year);
    }
    else
    {
        printf("%d is not a leap year\n", year);
    }

    return 0;
}

Output 1:
Enter a year
2020
2020 is a leap year

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

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 Find The Day on 01 January using Gregorian Calendar

According to the Gregorian Calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

Note: From 01/01 we understand that it’s 1st of January. We shall take the year as 1900. We also know that 01 January 1900 was Monday.

Related Read
while loop in C programming
C Program To Check Leap Year
Simple Calculator Program using Switch Case: C

Gregorian Calendar: the calendar introduced in 1582 by Pope Gregory XIII, as a modification of the Julian Calendar.

Expected Output for the Input

User Input:
Enter a year between 1900 and 2099
2020

Output:
The day on 01 January 2020 was Wednesday.

Video Tutorial: C Program To Find The Day on 01 January in Gregorian Calendar


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

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

Logic To Find The Day on 01 January using Gregorian Calendar

We take reference year as 1900. We ask the user to enter a year between 1900 and 2099 and store it inside the address of variable year. Now we find the difference (1900 – year) and store it inside variable diff.

Using while loop, we loop through until reference year(1900) is less than the user entered year. Inside the while loop we check for all the leap years present from 1900 to user entered year.

Next we calculate exact number of days between 1900 and user entered year using formula:

diff = ref_year – year;
Total no of leap years is calculated and stored in variable leap.

Total_days = (diff – leap) x 365 + leap x 366;

Now every year has 12 months, but each month has different number of days. But every week has exactly 7 days. So we divide Total_days by 7 and store the reminder inside variable day.

Note: Since our reference year 1900 has Monday on 1st of January. So we take Monday as first day:

0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday

We make use of Switch case to match the number in variable day to the day of the week.

Source Code: C Program To Find The Day on 01 January using Gregorian Calendar

#include < stdio.h >

int main()
{
    int ref_year = 1900, year, leap = 0, diff, total_days = 0, day = 0;

    printf("Enter a year between 1900 and 2099\n");
    scanf("%d", &year);

    diff = year - ref_year;

    while(ref_year < year)
    {
        if(ref_year % 100 == 0)
        {
            if(ref_year % 400 == 0)
            {
                leap++;
            }
        }
        else
        {
            if(ref_year % 4 == 0)
            {
                leap++;
            }
        }
        ref_year++;
    }

    total_days = (diff - leap) * 365 + leap * 366;
    day        = total_days % 7;

    printf("\nThe day on 01 January %d was ", year);

    switch(day)
    {
        case 0: printf("Monday.\n");
                break;
        case 1: printf("Tuesday.\n");
                break;
        case 2: printf("Wednesday.\n");
                break;
        case 3: printf("Thursday.\n");
                break;
        case 4: printf("Friday.\n");
                break;
        case 5: printf("Saturday.\n");
                break;
        case 6: printf("Sunday.\n");
                break;
    }

    return 0;
}

Output 1:
Enter a year between 1900 and 2099
1900

The day on 01 January 1900 was Monday.

Output 2:
Enter a year between 1900 and 2099
2015

The day on 01 January 2015 was Thursday.

Output 3:
Enter a year between 1900 and 2099
2016

The day on 01 January 2016 was Friday.

Output 4:
Enter a year between 1900 and 2099
2017

The day on 01 January 2017 was Sunday.

Output 5:
Enter a year between 1900 and 2099
2018

The day on 01 January 2018 was Monday.

Lets Use Ternary / Conditional Operator To Find Leap Year

We modify above program and use Ternary Operator or Conditional Operator to find leap year. You can find detailed explanation of finding leap year using Conditional Operator here: C Program To Check Leap Year Using Ternary Operator

Source Code: C Program To Find The Day on 01 January using Gregorian Calendar Using Ternary or Conditional Operator

#include < stdio.h >

int main()
{
    int ref_year = 1900, year, leap = 0, nonleap = 0, total_days = 0, day = 0;

    printf("Enter a year between 1900 and 2099\n");
    scanf("%d", &year);

    while(ref_year < year)
    {
        (ref_year % 100 == 0) ?
        ( (ref_year % 400 == 0)?
          (leap++):
          (nonleap++)
        ) :
        ( (ref_year % 4 == 0)?
          (leap++):
          (nonleap++)
        );

        ref_year++;
    }

    total_days = nonleap * 365 + leap * 366;
    day        = total_days % 7;

    printf("\nThe day on 01 January %d was ", year);

    switch(day)
    {
        case 0: printf("Monday.\n");
                break;
        case 1: printf("Tuesday.\n");
                break;
        case 2: printf("Wednesday.\n");
                break;
        case 3: printf("Thursday.\n");
                break;
        case 4: printf("Friday.\n");
                break;
        case 5: printf("Saturday.\n");
                break;
        case 6: printf("Sunday.\n");
                break;
    }

    return 0;
}

Output 1:
Enter a year between 1900 and 2099
2019

The day on 01 January 2019 was Tuesday.

Output 2:
Enter a year between 1900 and 2099
2020

The day on 01 January 2020 was Wednesday.

Output 3:
Enter a year between 1900 and 2099
2021

The day on 01 January 2021 was Friday.

Output 4:
Enter a year between 1900 and 2099
2022

The day on 01 January 2022 was Saturday.

Output 5:
Enter a year between 1900 and 2099
2023

The day on 01 January 2023 was Sunday.

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