Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Se dau datele de nastere a 2 studenti in formatul luna/zi/an.
- a. Cine este mai in varsta?
- b. Cu cati ani/luni/zile este mai in varsta?
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void findAge(int current_date,int current_month,
- int current_year, int birth_date,
- int birth_month, int birth_year)
- {
- int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
- if(birth_date > current_date){
- current_date = current_date + month[birth_month - 1];
- current_month = current_month - 1;
- }
- if(birth_month > current_month){
- current_year = current_year - 1;
- current_month = current_month + 12;
- }
- int calculated_date = current_date - birth_date;
- int calculated_month = current_month - birth_month;
- int calculated_year = current_year - birth_year;
- printf("\nYour age:\n\nYears: %d \nMonths: %d \nDays:"
- "%d\n", calculated_year, calculated_month, calculated_date);
- }
- int main()
- {
- time_t rawtime;
- struct tm *info;
- time( &rawtime );
- info = localtime( &rawtime );
- //printf("Current local time and date: %s", asctime(info));
- int birth_date, birth_month, birth_year, age_difference_years, age_difference_months, age_difference_days;
- int *current_date, *current_month, *current_year;
- current_date = info -> tm_mday;
- current_month = info -> tm_mon + 1;
- current_year = info -> tm_year + 1900;
- printf("\nPerson 1. \n");
- printf("\nWhat day were you born?\n");
- printf("Date = ");scanf("%d", &birth_date);
- printf("What month were you born?\n");
- printf("Month = ");scanf("%d", &birth_month);
- printf("What year were you born?\n");
- printf("Year = " );scanf("%d", &birth_year);
- int birth_date1 = birth_date;
- int birth_month1 = birth_month;
- int birth_year1 = birth_year;
- findAge(current_date,current_month, current_year,
- birth_date, birth_month, birth_year);
- printf("\n\nPerson 2. \n");
- printf("\nWhat day were you born?\n");
- printf("Date = ");scanf("%d", &birth_date);
- printf("What month were you born?\n");
- printf("Month = ");scanf("%d", &birth_month);
- printf("What year were you born?\n");
- printf("Year = " );scanf("%d", &birth_year);
- findAge(current_date,current_month, current_year,
- birth_date, birth_month, birth_year);
- if(birth_year > birth_year1)
- age_difference_years = birth_year - birth_year1;
- else
- age_difference_years = birth_year1 - birth_year;
- if(birth_month > birth_month1)
- age_difference_months = birth_month - birth_month1;
- else
- age_difference_months = birth_month1 - birth_month;
- if(birth_date > birth_date1)
- age_difference_days = birth_date - birth_date1;
- else
- age_difference_days = birth_date1 - birth_date;
- printf("\nThe difference between your ages is: \n\nYears: %d \nMonths: %d\nDays: %d\n",
- age_difference_years, age_difference_months, age_difference_days);
- if(birth_year1 == birth_year )
- if(birth_month1 == birth_month)
- if(birth_date1 == birth_date)
- printf("\nBoth people are the same age.");
- else if(birth_date1 > birth_date)
- printf("\nPerson 2 is older.");
- else
- printf("\nPerson 1 is older.");
- else if(birth_month1 > birth_month)
- printf("\nPerson 2 is older.");
- else
- printf("\nPerson 1 is older.");
- else if(birth_year1 > birth_year)
- printf("\nPerson 2 is older.");
- else
- printf("\nPerson 1 is older.");
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement