Reputation:
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.
Upvotes: 2
Views: 1274
Reputation: 24156
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;
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:
=> am = cm - bm + 12 => 12 - bm + cm
Upvotes: 2
Reputation: 504
#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();
}
Upvotes: 0
Reputation: 876
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:
Good luck, hopefully this explains things well enough.
Upvotes: 0