Guys i cant imagine about how i'm gonna calculate that one... help me out please

Dani AI

Generated

A concise, robust approach is calendar arithmetic: count full years first, then remaining full months, then remaining days. That method matches how people think about “age” (years/months/days) and avoids trying to convert a raw day-count into months, which is error-prone because month lengths vary. is right that many examples exist online; provided a workable C sample, but input validation and careful leap-day/month-boundary handling deserve explicit attention.

Algorithm (high level):

  • Parse birth date (by,bm,bd) and obtain the current date (cy,cm,cd).
  • Years = cy - by; if (cm < bm) or (cm == bm and cd < bd) then Years--.
  • Months = cm - bm; if cd < bd then Months--. If Months < 0 then Months += 12.
  • Days = cd - bd; if cd < bd then borrow days from the previous calendar month (use that month’s length, accounting for leap years).

A short C helper implementing that logic:

int days_in_month(int y, int m) {
    if (m == 2) {
        int leap = (y%4==0 && (y%100!=0 || y%400==0));
        return leap ? 29 : 28;
    }
    int mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    return mdays[m-1];
}

void calc_age(int by,int bm,int bd,int cy,int cm,int cd,int *Y,int *M,int *D) {
    int y = cy - by;
    int m = cm - bm;
    int d = cd - bd;
    if (d < 0) {
        int prev_m = cm - 1;
        int prev_y = cy;
        if (prev_m == 0) { prev_m = 12; prev_y--; }
        d += days_in_month(prev_y, prev_m);
        m--;
    }
    if (m < 0) { m += 12; y--; }
    *Y = y; *M = m; *D = d;
}

Notes and cautions: validate input (illegal dates like 2016-02-30), decide how to treat Feb 29 births in non-leap years and include tests for leap-day, same-day, and month-end cases. For Pascal/Delphi, the DateUtils unit provides routines (YearsBetween, MonthsBetween, DaysBetween, IncMonth) that simplify this same logic.

Recommended Answers

All 2 Replies

Here is one solution for your problem that i found.

#include <stdio.h>
  #include <time.h>

  /* checks whether the given input is leap year feb or not */
  int leapYearFeb(int year, int mon) {
        int flag = 0;
        if (year % 100 == 0) {
                if (year % 400 == 0) {
                        if (mon == 2) {
                                flag = 1;
                        }
                }
        } else if (year % 4 == 0) {
                if (mon == 2) {
                        flag = 1;
                }
        }
        return (flag);
  }

  int main() {
        /* days in months */
        int daysInMon[] = {31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31};
        int days, month, year;
        char dob[100];
        time_t ts;
        struct tm *ct;

        /* get the date of birth from the user */
        printf("Enter your days of birth:");
        fgets(dob, 100, stdin);
        sscanf(dob, "%d/%d/%d", &days, &month, &year);
        ts = time(NULL);
        ct = localtime(&ts);
        /* scan the year, month and year from the input string */
        printf("Current Date: %d/%d/%d\n",
                ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);

        days = daysInMon[month - 1] - days + 1;

        /* checking whether month is leap year feb - 29 days*/
        if (leapYearFeb(year, month)) {
                days = days + 1;
        }

        /* calculating the no of days, years and months */
        days = days + ct->tm_mday;
        month = (12 - month) + (ct->tm_mon);
        year = (ct->tm_year + 1900) - year - 1;

        /* checking for leap year feb - 29 days */
        if (leapYearFeb((ct->tm_year + 1900), (ct->tm_mon + 1))) {
                if (days >= (daysInMon[ct->tm_mon] + 1)) {
                        days = days - (daysInMon[ct->tm_mon] + 1);
                        month = month + 1;
                }
        } else if (days >= daysInMon[ct->tm_mon]) {
                days = days - (daysInMon[ct->tm_mon]);
                month = month + 1;
        }

        if (month >= 12) {
                year = year + 1;
                month = month - 12;
        }

        /* print the result */
        printf("Age: %d year %d months and %d days\n", year, month, days);
        return 0;
  }

From Here i got the answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.