# 5+ C Age Calculator Programs

This post contains a total of 5+ C Age Calculator Program Examples with Source Code. All these Age Calculator Programs are made using C.

You can use the source code of these examples with credits to the original owner.

## C Age Calculator Programs

### 1. Detailed Age Calculator

Made by Brain & Bones. Source

```Input - 01 01 2002

The current date is 08-25-2022

You are 19 years, 18 months, and 24 days old.```
``````#include <stdio.h>
#include <time.h>

int main (void){
int month, day, year, monthT, dayT, yearT;
scanf("%2d%2d%4d", &month, &day, &year);
time_t seconds = time(NULL);
struct tm *now = localtime(&seconds);
monthT = now->tm_mon + 1;
dayT = now->tm_mday;
yearT = now->tm_year + 1900;
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(yearT % 4 == 0){
days[1] = 29;
}

int day_month = days[now->tm_mon];
int month_before = days[month - 2];
if(month == 0){
month_before = days[12];
}
printf("The current date is %02d-%02d-%d\n", monthT, dayT, yearT);
if(month < monthT)
if(day > dayT)
printf("\nYou are %d years, %d months, and %d days old.", yearT - 1 - year, 12 - month + now->tm_mon - 1, dayT + month_before - day);
else
printf("\nYou are %d years, %d months, and %d days old.", yearT - 1 - year, 12 - month + now->tm_mon, dayT - day);

else if(month > monthT)
if(day > dayT)
printf("\nYou are %d years, %d months, and %d days old.", yearT - 1 - year, 12 - month + now->tm_mon - 1, dayT + month_before - day);
else
printf("\nYou are %d years, %d months, and %d days old.", yearT - 1 - year, 12 - month + now->tm_mon, dayT - day);

else
if(day > dayT)
printf("\nYou are %d years, %d months, and %d days old.", yearT - 1 - year, 11, day_month - (day - dayT));
else
printf("\nYou are %d years, %d months, and %d days old.", yearT - year, 0, dayT - day);

}``````

### 2. Age calculator using function

Made by Jawad Shopnil. Simple C Age Calculator Program. The program calculates the age using function, First input your born year and then input the current year. Source

```2002
2022
Result: Your age is 20 years```
``````#include <stdio.h>

int age(int a, int b);

int main()
{
int y, y2, r;
scanf("%d%d", &y, &y2);
r = age(y, y2);
printf("if you like the code!!! \n");
printf("\n");
printf("Result: Your age is %d years", r);
return 0;
}

int age(int a, int b)
{
int age2;
age2 = b - a;
return(age2);
}``````

### 3. Age Calculator by Chandan Ghosh

Made by Chandan Ghosh. Simple Program to calculate age in year, month and day. Format is todays date and after that birthdate. Source

```Today's date:       2022/08/25
__________________________________
20 Years 7 Months 24 Days```
``````#include <stdio.h>

int main()
{
int y1,m1,d1;

scanf("%4d/%2d/%2d",&y1,&m1,&d1);

printf("Today's date:       %4d/%02d/%02d\n",y1,m1,d1);

int y,m,d;

scanf("%4d/%2d/%2d",&y,&m,&d);

printf("__________________________________\n");

int d2=d1-d;
int m2=m1-m;
int y2=y1-y;

int d3=((30+d1)-d);
int m3=m1-1;
int m4=m3+12;
int m5=m4-m;
int m6=m3-m;
int m7=(m1+12)-m;
int y3=y1-1;
int y4=y3-y;

if(m1>m && d1>d)
printf("%d Years %d Months %d Days",y2,m2,d2);

else if(m1<m && d1>d)
printf("%d Years %d Months %d Days",y4,m5,d2);

else if(m1<m && d1<d)
printf("%d Years %d Months %d Days",y4,m5,d3);

else if(m1==m && d1<d)
printf ("%d Years %d Months %d Days",y4,m5,d3);

else if(m1>m && d1<d)
printf ("%d Years %d Months %d Days",y2,m6,d3);

else if(m1==m && d1==d)
printf ("%d Years %d Months %d Days",y2,m2,d2);

else if(m1<m && d1==d)
printf ("%d Years %d Months %d Days",y4,m7,d2);

return 0;
}``````

### 4. Age Calculator in C

```01/01/2002

Current Date: Aug 25 2022

You are  20 years 7 months and 25 days old```
``````#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;
time_t ts;
struct tm *ct;

scanf("%d/%d/%d",&days,&month, &year);

ts = time(NULL);
ct = localtime(&ts);

printf("Current Date: %s", __DATE__);

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("\n\nYou are  %d years %d months and %d days old\n", year, month, days);

return 0;
}``````

### 5. Age Calculator by Jeevan Chandra Joshi

Made by Jeevan Chandra Joshi. Source

```Enter current date in DD MM YYYY :  25/8/2022
Enter birth date in DD MM YYYY :  1/1/2002
Present age is 20 Years, 7 Months and 24 Days```
``````#include<stdio.h>
#include<stdlib.h>

int curDate, curMonth, curYear;
int birDate, birMonth, birYear;
int calDate, calMonth, calYear;

void Age(int curDate, int curMonth, int curYear, int birDate, int birMonth, int birYear);

int main()
{
printf("Enter current date in DD MM YYYY : ");
scanf("%d %d %d", &curDate, &curMonth, &curYear);
printf(" %d/%d/%d", curDate, curMonth, curYear);
printf("\nEnter birth date in DD MM YYYY : ");
scanf("%d %d %d", &birDate, &birMonth, &birYear);
printf(" %d/%d/%d", birDate, birMonth, birYear);

Age(curDate, curMonth, curYear, birDate, birMonth, birYear);

printf("\nPresent age is %d Years, %d Months and %d Days", calYear, calMonth, calDate);

return 0;
}

void Age(int curDate, int curMonth, int curYear, int birDate, int birMonth, int birYear)
{
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if(birDate > curDate)
{
curDate = curDate + month[birMonth - 1];
curMonth = curMonth - 1;
}

if(birMonth > curMonth)
{
curYear = curYear - 1;
curMonth = curMonth + 12;
}

if(birYear > curYear)
{
printf("\nAge cannot be negative");
exit(0);
}

calDate = curDate - birDate;
calMonth = curMonth - birMonth;
calYear = curYear - birYear;
} ``````

### 6. Age calculator using C

```Enter birth date in DD MM YYYY Format
Date:1/1/2002
Enter today's date in DD MM YYYY Format
Date:25/8/2022

You are 20 Years 7 Months and 24 days older!```
``````#include<stdio.h>
int valday(int,int,int);
int valmonth(int);
int valyear(int);
int leap(int);
int main(){
int d1,m1,y1,l1;
int d2,y2,m2,l2;
int cy,cm,cd,cdate1,cdate2;
int day=0,month=0,year=0,er=0,c1=0,c2=0;

flag1:
printf("Enter birth date in DD MM YYYY Format\n");
scanf("%d%d%d",&d1,&m1,&y1);
cy=0,cm=0,cd=0,cdate1=0;
if(valyear(y1)){
cy++;
l1=leap(y1);
}
if(valmonth(m1)) cm++;
if(valday(d1,m1,l1)) cd++;
if(cy!=0&&cm!=0&&cd!=0) cdate1++;
else{
printf("\nEntered date is invalid!!\nEnter Again\n");
goto flag1;
}
printf("Date:%d/%d/%d\n",d1,m1,y1);
cy=0,cm=0,cd=0,cdate2=0;
flag2:
printf("Enter today's date in DD MM YYYY Format\n");
scanf("%d%d%d",&d2,&m2,&y2);
if(valyear(y2)){
cy++;
l2=leap(y2);
}
if(valmonth(m2)) cm++;
if(valday(d2,m2,l2)) cd++;
if(cy!=0&&cm!=0&&cd!=0) cdate2++;
else{
printf("\nToday's date is invalid!!\n");
goto flag2;
}
printf("Date:%d/%d/%d",d2,m2,y2);
if(y2==y1){
if(m2==m1){
if(d2>d1) c1++;
if(d2==d1) c1++;
if(d2<d1) er++;
}
else if(m2>m1){
if(d2>=d1) c1++;
if(d2<d1) c2++;
}
else er++;
}
else if(y2>y1){
if(m2==m1){
if(d2>=d1) c1++;
else{
year=y2-y1-1;
month=11;
day=(30-d1)+d2;
}
}
else if(m2>m1)
if(d2>=d1) c1++;
else c2++;
else
if(d2>=d1){
day=d2-d1;
year=y2-y1-1;
month=(12-m1)+m2;
}
else{
year=y2-y1-1;
month=(12-m1)+m2-1;
day=(30-d1)+d2;
}
}
else er++;
if(c1!=0){
day=d2-d1;
month=m2-m1;
year=y2-y1;
}
if(c2!=0){
year=y2-y1;
month=m2-m1-1;
day=(30-d1)+d2;
}
if(er==0) printf("\n\nYou are %d Years %d Months and %d days older!",year,month,day);
else printf("\n\nCurrent date should be greater!!");
return 0;
}

int leap(int year){
if((year%4==0)&&(year%100!=0)) return 1;
else if(year%400==0) return 1;
else return 0;
}
int valyear(int year){
if(year>=1000&&year<=9999) return 1;
else return 0;
}
int valmonth(int month){
if(month>=1 && month<=12) return 1;
else return 0;
}
int valday(int day,int month,int leap){
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
if((day>=1)&&(day<=31)) return 1;
else return 0;
else{
if((month==4)||(month==6)||(month==9)||(month==11))
if((day>=1)&&(day<=30)) return 1;
else return 0;
else{
if(leap==1)
if((day>=1)&&(day<=29)) return 1;
else return 0;
else
if((day>=1)&&(day<=28)) return 1;
else return 0;
}
}
}``````