Saturday, October 7, 2017

Date Sum Using Structure in C/C++ Language

Problem

Create a struct called data. User should be able to input day, month, and year. Take two date inputs and add them. Display the result.

Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

//struct decleration
struct data {
int years;
int months;
int days;
}d1,d2;

//function protorypes
void add_data();
void add_dates();
void display(int ,int ,int);

int main()
{
    //function calls
    add_data();
    add_dates();
   
printf ("\n\nPress any key to exit.");
getch();
return 0;
}

//adding record
void add_data()
{
     //1st date
     printf ("Enter 1st date.");
   
     printf ("\n\nEnter year: ");
     scanf ("%d",&d1.years);
   
     printf ("\nEnter month: ");
     scanf ("%d",&d1.months);
   
     printf ("\nEnter day: ");
     scanf ("%d",&d1.days);
   
     display(d1.days,d1.months,d1.years);
     //printf ("\n\nThe 1st date is %d/%d/%d",d1.days,d1.months,d1.years);
   
     //Second time
     printf ("\n\n\nNow, please type the 2nd date.");
   
     printf ("\nEnter year: ");
     scanf ("%d",&d2.years);
   
     printf ("\nEnter month: ");
     scanf ("%d",&d2.months);
   
     printf ("\nEnter day: ");
     scanf ("%d",&d2.days);
   
     display(d2.days,d2.months,d2.years);
}

void display (int a,int b,int c)
{
     printf("\nThe date is %d/%d/%d \n\n", a,b,c);
}   

void add_dates()
{
     int y,m,d;
   
     y = d1.years + d2.years;
     m = d1.months + d2.months;
     d = d1.days + d2.days;
   
     printf ("\n\n\nSum of the date's is %d/%d/%d",d,m,y);
}

Output

Date Sum Using Structure in C/C++ Language
Date Sum Using Structure in C/C++ Language

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact