Program in C++ to Calculate Age
#include<iostream> #include<conio.h> using namespace std; int main() { int y,m,d,td,pd,tm,pm,ty,py; cout<<"Enter Previous Day, Month and Year:-"; cin>>pd>>pm>>py; cout<<"Enter today Day , Month and Year:-"; cin>>td>>tm>>ty; int mm[12]={31,py%4==0&&py%100!=0||py%400==0?29:28,31,30,31,30,31,31,30,31,30,31}; if(pd>td) //For Dates { td=td+mm[tm-2]; d=td-pd; tm=tm-1; } else d=td-pd; if(pm>tm) //For Months { tm=tm+12; ty=ty-1; m=tm-pm; } else m=tm-pm; y=ty-py; //For Years cout<<"\nAge is "<<y<<" Years "<<m<<" Months and "<<d<<" Days."; return 0; }
Output:-
3 Comments