-
Notifications
You must be signed in to change notification settings - Fork 121
/
Agecalculator.c
81 lines (67 loc) · 1.71 KB
/
Agecalculator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <time.h>
int isLeapYear(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()
{
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;
printf("Enter your date of birth (DD/MM/YYYY): ");
scanf("%d/%d/%d",&days,&month, &year);
ts = time(NULL);
ct = localtime(&ts);
printf("Current Date: %d/%d/%d\n",
ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
days = DaysInMon[month - 1] - days + 1;
if (isLeapYear(year, month))
{
days = days + 1;
}
days = days + ct->tm_mday;
month = (12 - month) + (ct->tm_mon);
year = (ct->tm_year + 1900) - year - 1;
if (isLeapYear((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;
}
printf("\nHey you are %d years %d months and %d days old.\n", year, month, days);
return 0;
}