Skip to content

Instantly share code, notes, and snippets.

@RhythmShahriar
Created February 8, 2014 06:52
Show Gist options
  • Save RhythmShahriar/8877726 to your computer and use it in GitHub Desktop.
Save RhythmShahriar/8877726 to your computer and use it in GitHub Desktop.
/*
PROJECT : BIRTHDAY CALCULATOR
PROGRAMMER : Rhythm Shahriar
URL : http://rhythmshahriar.com
EMAIL : [email protected]
COPYRIGHT : copyright © - 2014 BULKSTUDIO INC.
*/
#include <stdio.h>
#include <time.h>
/* checks whether the given input is leap year feb or not */
int lYear(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 dInM[] = {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 [day/month/year]: ");
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("\nCurrent Date: %d/%d/%d\n",
ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
days = dInM[month - 1] - days + 1;
/* checking whether month is leap year feb - 29 days*/
if (lYear(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 (lYear((ct->tm_year + 1900), (ct->tm_mon + 1))) {
if (days >= (dInM[ct->tm_mon] + 1)) {
days = days - (dInM[ct->tm_mon] + 1);
month = month + 1;
}
} else if (days >= dInM[ct->tm_mon]) {
days = days - (dInM[ct->tm_mon]);
month = month + 1;
}
if (month >= 12) {
year = year + 1;
month = month - 12;
}
/* print the result */
printf("\nYour age: %d year %d months and %d days\n\n",year, month, days);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment