2

We are given Birthdate and Currentdate as input and we have to find age in years, months and days, ignoring leap years.

Here is my current code (its incomplete and some things are missing) :

#include <stdio.h>

void main() {
    int cy, cm, cd, by, bm, bd, ay, am, ad;
    int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    printf("\nEnter Current Date (dd/mm/yyyy) ->  ");
    scanf("%d/%d/%d", &cd, &cm, &cy);
    printf("\nEnter Birth Date (dd/mm/yyyy) ->  ");
    scanf("%d/%d/%d", &bd, &bm, &by);
    if (cm > bm) {
        ay = cy - by;
        //bm = month[bm - 1];
    } else
    if (cm < bm) {
        ay = cy - by - 1;
    } else {
        if (bd >= cd) {
            ay = cy - by;
            ad = bd - cd;
        } else {
            ay = cy - by - 1;
            ad = cd - bd;
        }
    }
}

How should I go about calculating days and months? I don't want to use libraries like <time.h> etc. and want to do it manually first.

13
  • 2
    Just on the side, did your teacher tell you to write void main()?
    – DeiDei
    Oct 23, 2016 at 14:40
  • 1
    This is much harder than it looks. Why don't you want to use the relevant standard library functions (mktime, strptime, difftime)? This book will show you just how hard it is.
    – zwol
    Oct 23, 2016 at 14:43
  • 5
    @M.SIngh You must use int main and you must end by returning 0 (specifically 0, until you get to the point where you care about indicating whether the program succeeded or failed). void main is accepted as an extension by some C compilers but is Wrong.
    – zwol
    Oct 23, 2016 at 14:45
  • 1
    So this is not about the language C, but an algorithm. I'm sure a simple search will show you a long list of matches. Oct 23, 2016 at 14:54
  • 1
    I thought that DOB (Date of Birth) is "Birthdate", so you are done. Oct 23, 2016 at 15:04

3 Answers 3

2

taking @Jacob answer as starting point, you can implement this in following way:

ay = cy - by;
am = cm - bm;
ad = cd - bd;

if (ad < 0) {
    am--; // one month is not full month
    ad = month[bm - 1] - bd + cd; // can be simplified to ad += month[bm - 1];
}

if (am < 0) {
    ay--; // one year is not full year
    am = 12 - bm + cm; // am += 12; will work too
}

explanation of ad = month[bm - 1] - bd + cd;

  1. imagine two dates: 10/1/2000 - 5/2/2000, ad will be (5-10)=-5
  2. so, instead of wrong answer you should calculate days in january (31-10) and days in february 5 = 26

logic behind am = 12 - bd + cm is the same as for days, we need to calculate how many month was in previous year and add current month

why we can simply am calculation:

  1. am = cm - bm;
  2. am += 12;

=> am = cm - bm + 12 => 12 - bm + cm

2
  • 1
    Suggest simplification ad = month[bm - 1] - bd + cd; --> ad += month[bm - 1]; Oct 23, 2016 at 17:13
  • @Lashane: Can you contact me, please — see my profile? I have some issues to discuss that don't discuss well in comments (too much code, data). The issue is 'what value do you get from entering these dates?' Following what I think is your algorithm, I got: Date 2017-01-27; DoB 2016-02-27; Age 0 y, 11 m, 0 d —— Date 2017-01-27; DoB 2016-02-28; Age 0 y, 11 m, 27 d where a difference of 1 day in the date of birth (and same current date) is yielding 27 days difference in age. I assume I have something wrong, but I'd like to check. Oct 24, 2016 at 0:34
0

It sounds like what you need is an age calculator, and it doesn't need to deal with leap years or any other problem concepts.

What you are looking for right now is just difference. Let's start with exactly that:

date: 10/11/2016
birth date: 4/8/2012
result == 2016-2012 years, 11-8 months, 10-4 days
== 4 years, 7 months, 6 days

And so with 3 simple subtractions, 4 years, 7 months, 6 days is the result. This is the easy part, in fact we can always start with this simple subtraction, this is step 1.

Now let us consider another case:

date: 10/4/2016
birth date: 21/9/2001
== 15 years, -5 months, -11 days

What we have is still technically correct, but you probably don't want negatives in your answer. There is a really easy fix to this: a year is the next unit up from months, so we can just move one year to the months column (this should look familiar, think back to grade 6 math).

   15y,-5m -11d
== 14y, -5m+12m, -11d
== 14y, 7m, -11d

Starting to see how this works? The last and hardest step is the days. Because there is variation in number of days between month, we need to check how many days were in the current month. In your code you use month[], so I will use this array name:

   14y, 7m, -11d
== 14y, 6m, -11d+month[7-1]
== 14y, 6m, -11d+31d
== 14y, 6m, 20d

And there you have your final answer, 14 years, 6 months, 20 days. There is only one case in which you could get a negative age now, and that is when the birth date entered is after the current date; you can choose to handle that however you like. There is one more case that could give you issues using this algorithm: you could get 0s. I will let you figure that out though, because I don't want to do all of your work for you here.

So to sum everything up:

  1. Subtract the birth date from the current date
  2. If the resultant months are negative, add 12 and subtract 1 from the resultant years
  3. If the resultant days are negative, add month[resultant months-1] days to them, and subtract 1 from resultant months

Good luck, hopefully this explains things well enough.

1
  • Well explained but has trouble with "add month[resultant months-1] days". That may only add 28, yet the difference was -30. Oct 23, 2016 at 17:12
0
#include<stdio.h>
#include<conio.h>
main()
{
int cd,cm,cy,bd,bm,by;
int y=0, m=0 ,d=0;
clrscr();
scanf("%d%d%d",&bd,&bm,&by);
scanf("%d%d%d",&cd,&cm,&cy);

if(cd <= bd)
{
d = cd + 30 - bd;
m--;
}
else
d = cd - bd;


if( cm <= bm )
{
m = m +(cm +12 - bm);
y--;
}
else
m = cm - bm;

y = y +(cy - by);


if(d==30 && m==11)
{
d=0;
m=0;
y++;
}


printf(" %d %d %d",d,m,y);
getch();
}
3
  • this program assumes a month to be 30 days. Oct 23, 2016 at 15:41
  • In the standard Gregorian calendar, some months have 30 days, some have 31, one has 28 or 29. We won't go into discussions of ancient calendars (30th February 1712 — in Sweden; 19 days in September 1752 in.Britain and its colonies; etc.). Oct 23, 2016 at 22:35
  • if we want that to be implemented, we need to replace 30 in the program with: months[cm-1]. where months is: int monts[12] = {31,0,31,30.....}; if(cy%4 ==0) months[1]=29; else months[1]=28; Oct 24, 2016 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.