C Examples

WAP in C that uses a function that takes two date (day, month, year) structure objects as arguments and returns the structure with later date

The Program in C that uses a function that takes two date (day, month, year) structure objects as arguments and returns the structure with later date is given below:

#include <stdio.h>

struct Date {
    int day;
    int month;
    int year;
};

struct Date getLaterDate(struct Date d1, struct Date d2) {
    if (d1.year > d2.year) {
        return d1;
    } else if (d1.year == d2.year) {
        if (d1.month > d2.month) {
            return d1;
        } else if (d1.month == d2.month) {
            if (d1.day > d2.day) {
                return d1;
            } else {
                return d2;
            }
        } else {
            return d2;
        }
    } else {
        return d2;
    }
}

int main() {
    struct Date date1, date2, later_date;
    printf("Enter the first date (dd mm yyyy): ");
    scanf("%d %d %d", &date1.day, &date1.month, &date1.year);
    printf("Enter the second date (dd mm yyyy): ");
    scanf("%d %d %d", &date2.day, &date2.month, &date2.year);
    later_date = getLaterDate(date1, date2);
    printf("Later date is: %d/%d/%d\n", later_date.day, later_date.month, later_date.year);
    return 0;
}

Output:

Enter the first date (dd mm yyyy): 08
08
1999
Enter the second date (dd mm yyyy): 09
05
1997
Later date is: 8/8/1999

Pro-Tips💡

This program defines a struct “Date” with three members: day, month, and year. It then defines a function “getLaterDate()” that takes two structs of type “Date” as input and compares them by their year, month, and day members.


It returns the struct that has the later date.


In the main function, it prompts the user to input two dates, it stores them in two structs of type “Date” and then pass them as input to the “getLaterDate()” function.


The function compares the two dates and returns the struct that has the later date. The program then prints the later date in the format of day/month/year.


Please note that the program uses scanf(“%d %d %d”,…) to read the input date, and it does not check for the validity of the input, you may want to add some validation on the input to make sure that the input is a valid date.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X