# 7+ CPP Age Calculator Programs

This post contains a total of 7+ CPP 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.

## CPP Age Calculator Programs

### 1. Ace’s Age Calculator

Made by Ace. A fun birth date calculator that shows you roughly how old you are in seconds, minutes, hours, days, weeks, and years. Source

```What month were you born in (1-12): 01
What day of the month you were born: 01
What year you were born (After 1970 please): 2002
One moment.
Calculating...

You are 651597712 seconds old.
You are 10859961 minutes old.
You are 180999 hours old.
You are 7541 days old.
You are 1077 weeks old.
You are ~247 months old.
You are 20 years old.```
``````#include <climits>
#include <cmath>
#include <ctime>
#include <iostream>

bool leapYear(int year)
{
return ((year%4==0)&&(year%100!=0))||(year%400==0);
}

//checks if we have a valid date
bool validDate(int day, int month, int year)
{
if(month < 1 || year < 1970)
return false;
/*
* Remember the old poem:
* 30 days hath September,
* April, June and November;
* February has 28 alone
* All the rest have 31
* Except in Leap Year, that's the time
* When February's Days are 29
*/
switch(month)
{
case 1: return day > 0 && day < 31;
case 2: return leapYear(year) ? day > 0 && day < 29 : day > 0 && day < 28;
case 3: return day > 0 && day < 31;
case 4: return day > 0 && day < 30;
case 5: return day > 0 && day < 31;
case 6: return day > 0 && day < 30;
case 7: return day > 0 && day < 31;
case 8: return day > 0 && day < 31;
case 9: return day > 0 && day < 30;
case 10: return day > 0 && day < 31;
case 11: return day > 0 && day < 30;
case 12: return day > 0 && day < 31;
default: return false;
}
}

//the UI
int askForBirthdate(int &day, int &month, int &year)
{
//Get birthday input from user.
std::cout << "Welcome to the Ace's age Calculator!\n";
std::cout << "\n\nWhat month were you born in (1-12): ";
std::cin >> month;
if(std::cin.fail())
{
//std::cout << "\nmonth cin.fail() set.\n";
return 1;
}
{
return 1;
}
std::cout << "\nWhat day of the month you were born: ";
std::cin >> day;
if(std::cin.fail())
{
//std::cout << "\nday cin.fail() set.\n";
return 1;
}
{
return 1;
}
std::cout << "\nWhat year you were born (After 1970 please): ";
std::cin >> year;
if(std::cin.fail())
{
//std::cout << "\nyear cin.fail() set.\n";
return 1;
}
{
return 1;
}
if(validDate(day, month, year))
std::cout << "\nOne moment.\nCalculating...\n\n";
else
{
//std::cout << "\nvalidDate() fail.\n";
return 1;
}
return 0;

}

//time struct for birth date
time_t timeForDate(int day, int month, int year) {
struct tm timeinfo = { 0 };
timeinfo.tm_mday = day;
timeinfo.tm_mon = month - 1;
timeinfo.tm_year = year - 1900;
timeinfo.tm_isdst = -1; //is it daylight savings time?
return mktime(&timeinfo);
}

int main() {
int dayOfBirth, monthOfBirth, yearOfBirth;
try{
throw -1;
}catch(...)
{
std::cerr << "\nError. Could not read proper birth date. Quitting";
return 0;
}
time_t birthTime = timeForDate(dayOfBirth, monthOfBirth, yearOfBirth);
time_t nowTime = time(0);
time_t ageInSeconds = nowTime - birthTime;
std::cout << "You are " << ageInSeconds << " seconds old.\n";
std::cout << "You are " << ageInSeconds / 60 << " minutes old.\n";
std::cout << "You are " << ageInSeconds / 60 / 60 << " hours old.\n";
std::cout << "You are " << ageInSeconds / 60 / 60 / 24 << " days old.\n";
std::cout << "You are " << ageInSeconds / 60 / 60 / 24 / 7 << " weeks old.\n";
std::cout << "You are ~" << (int) (ageInSeconds / 60 / 60 / 24 / (double) 365.25 * 12) << " months old.\n";
std::cout << "You are " << ageInSeconds / 60 / 60 / 24 / 365 << " years old.\n";
return 0;
}``````

### 2. Age Calculator by Lakshay

Made by Lakshay. This will calculate your age in years, months and days. Enter date in this format: dd mm yyyy. Source

```Enter your D.O.B
01 01 2002
Your age is 20 years 7 months and 24 days```
``````#include <ctime>
#include <iostream>
using namespace std;

int main()
{
int d,m,y,day,mon,year;
time_t t = time(0);
struct tm *now = localtime(&t);
m=now->tm_mon+1;
d=now->tm_mday;
y=now->tm_year+1900;

cin>>day>>mon>>year;
d = d - day;
m = m - mon;
y = y - year;
if(d<0)
{
d=d+30;
m=m-1;
}
if(m<0)
{
m=m+12;
y=y-1;
}
if(y<0)
cout<<"Are you Joking?/nHow can you run this code before being born?";
else
cout<<"Your age is "<<y<<" years ";
if(m==0 && d==0)
cout<<"\nHappy Birthday!";
else
cout<<m<<" months and "<<d<<" days";
return 0;
}``````

### 3. Age Calculator by Karuma Tendai

Made by Karuma Tendai. The program calculates Age in Years. Source

```2002
Great ! ,You are  20 Years old ```
``````#include <iostream>
using namespace std;

int main() {
int in , calc , dob,pres = 2022;
cout<<"Hello User Enter Your Birth Year \n";
cin>>in;
calc = pres-in;
cout<<"Great ! ,You are  "<<calc << " Years old ";

return 0;
}``````

### 4. Age Calculator by Vaibhavi

Made by Vaibhavi. This C++ Program converts the years of age to months , days and hours. Source

```***your age is***
20
age in months 240
age in days 7300
age in hours 175200
age in seconds 630720000```
``````#include <iostream>
using namespace std;

int main()
{
int age,agemon,agedays,agehours,ageseconds;
cin>>age;
cout<<age<<endl;
agemon=age*12;
agedays=age*365;
agehours=agedays*24;
ageseconds=agehours*3600;
cout<<"age in months "<< agemon<<endl;
cout<<"age in days "<< agedays<<endl;
cout<<"age in hours "<<agehours<<endl;
cout<<"age in seconds "<<ageseconds<<endl;
return 0;
}``````

### 5. Age Calculator by priyanshu

Made by priyanshu. Simple Age Calculator program. Source

```age calculator
2002
``````#include<iostream>
using namespace std;
int  main()
{
float  a;
float  age;
float  b= 2022;

cout<<"age calculator"<<endl;

cin>> a;
age= b-a;

if(age< 18){

}

if(age< 0){
cout<<"something went wrong"<<endl;
}

return 0;
}``````

### 6. Age Calculator using c++

Made by Roabs. Basic Program to Calculate Age, Format of the Input should be : dd mm yyyy . Source

```2002
20 years
244 months
7331 days
1047 weeks
175944 hours
10556640 minutes
633398400 seconds```
``````#include <iostream>
#include <ctime>
using namespace std;

int main() {

time_t t = time(0);
struct tm * now = localtime(&t);

int years = (now->tm_year) + 1900;

int n1 = 0, n2 = 0, n3 = 0;
cin >> n1 >> n2 >> n3;
if(n3 > years)
{
cout << "are you from the futute?" << endl;
}
if(n1 > 31)
{
cout << "lol really? " << n1 << " days?" << endl;
}
if(n2 > 12)
{
cout << "more than 12 months?" << endl;
}
else
{
int year1, months1, days1; //converts everything to days
year1 = (years - n3) * 365;
months1 = n2 * 30;
days1 = n1;

const int all = year1 + months1 + days1;

unsigned long long int outY,outM,outD,outW,outH,outMi,outS;
outY = all / 365;
outM = all / 30;
outD = all;
outW = all / 7;
outH = all * 24;
outMi = all * 1440;
outS = all * 86400;
if(n1 > 0 && n2 > 0 && n3 > 0) {
cout << outY << " years" << endl
<< outM << " months" << endl
<< outD << " days" << endl
<< outW << " weeks" << endl
<< outH << " hours" << endl
<< outMi << " minutes" << endl
<< outS << " seconds" << endl;
}
else
{
cout << "write date of your birth" << endl << "in format:" << endl << "dd mm yyyy" << endl;
}
}
return 0;
}``````

### 7. Age Calculator by Prayash Gurung

Made by Prayash Gurung. You have to enter present date in d m yyyy format then enter your birth date in same manner. you need to enter 6 dates have have to press enter after entering each data. Source

```Present date day (dd): 25 Month : 8 year (yyyy): 2022
Birth date's day (dd): 1 Month (mm): 1 Year (yyyy): 2002
Your age as per the present day as dd/mm/yy= 24:7:20```
``````#include<iostream>
using namespace std;
int main ()
{int  d0,m0,y0,d1,m1,y1,d,m,y;

cout<<"    Present date day (dd): " ;
cin>>d0;
cout <<d0;
cout <<" Month : ";
cin>>m0;
cout<<m0;
cout <<" year (yyyy): ";
cin>>y0;
cout<<y0<<endl;

cout<<"    Birth date's day (dd): ";
cin>>d1;
cout<<d1; cout<<" Month (mm): ";
cin>>m1;
cout<<m1;
cout<<" Year (yyyy): ";
cin>>y1;
cout<<y1<<endl;
if (d0<d1)
{d=30+d0-d1;
cout<<"     Your age as as per the present day as dd/mm/yy= " <<d;}
else
{d=d0-d1;
cout<<"   Your age as per the present day as dd/mm/yy= " <<d;}

if ((d0<d1)&&(m0<m1))
{ m=m0-1+12-m1;
cout<<":"<<m;}
else if((d0<d1)&&(m0>m1))
{ m=m0-1-m1;
cout<<":"<<m;}
else if((d0>d1)&&(m0>m1))
{ m=m0-m1;
cout<<":"<<m;}
else if((d0>d1)&&(m0<m1))
{ m=m0+12-m1;
cout<<":"<<m;}
if(m0>m1)
{y=y0-y1;
cout<<":"<<y;}
else
{y=y0-1-y1;
cout<<":"<<y;}
return 0;

}
``````

### 8. Age Calculator by Sudhanshu Sharma

```2002
you are / you are turning20this year```
``````#include <iostream>
using namespace std;

int main() {
int a;
cin>>a;
a = 2022 -a;
cout<< "you are / you are turning"  <<  a  <<  "this year";

return 0;
}``````