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

Simple Calculator Program using Switch Case: C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program.
Simple Calculator Application In C

Simple Calculator using Switch Case: C Program


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

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


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.

If the user enters wrong choice, we ask the user to enter proper choice.

Calculator Program using Switch Case: C Program

#include < stdio.h >

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
    scanf("%c", &choice);


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


    switch(choice)
    {
        case 'a': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 'b': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 'c': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 'd': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output
Enter your choice
a. Addition
b. Subtraction
c. Multiplication
d. Division
c
Enter 2 integer numbers
5
10
5 x 10 = 50

In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.

#include < stdio.h >

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

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%d", &choice);

    if( choice > 4 )
    {
        printf("Select with in the range!\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    switch(choice)
    {
        case 1: printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 2: printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 3: printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 4: if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
10
2
10 / 2 = 5

Here we are asking user to enter 1 or 2 or 3 or 4 to choose the arithmetic operation.

#include

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%c", &choice);

    printf("Enter 2 integer numbers. Format: a + b\n");
    scanf("%d %c %d", &a, &choice, &b);



    switch(choice)
    {
        case '+': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case '-': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case '*': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case '/': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Enter 2 integer numbers. Format: a + b
50 + 60
50 + 60 = 110

Here we ask the user to enter values as well as the operation to be performed. Operands and operator.

Note 1: There is no need of curly braces inside case.
Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed.
Note 3: default case is optional. And the code inside it executes only when non of the cases match.
Note 4: Here switch, case, break, default are all keywords / reserve words.

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