Home ยป Student Services ยป Learn C++ ยป This source code calculates your current age in years, months and days.

This source code calculates your current age in years, months and days.

What is DOP and how calculates it in C++

It automatically gets current date from your computer and computes your date of birth.

Compiler is a type of System Software that is used to convert High Level Language into Low Level Language that computer can understands.

Following code is solved in Turbo C++ Compiler.

Question

Write a Program that inputs your age (year/month/days) and display how many years,months and days you are old in c++.

#include<iostream.h>
#include<conio.h>
#include<time.h>
int calcTotalDays(int y,int m,int d)
{
	int totalDays = (y *365) + (m *30) + d;
	cout<<"Total Days are = "<<totalDays<<endl;
	return totalDays;
}
int calcCurrentValue(char a[],int start,int end)
{
	int cy = (int)a[start] - 48 ;
	cy *= 10;
	cy += (int)a[end] - 48;

	return cy;
}
void main()
{
	clrscr();
	char date[9];
	_strdate(date);
	cout<<date<<endl;
	int cy = 2000 + calcCurrentValue(date,6,7);
	int cm = calcCurrentValue(date,0,1);
	int cd = calcCurrentValue(date,3,4);

	cout<<"\ncy"<<cy<<" cm = "<<cm<<" cd = "<<cd;

	clrscr();
	int by,bm,bd;
	cout<<"Enter your year of birth";
	cin>>by;
	cout<<"Enter your month of birth";
	cin>>bm;
	cout<<"Enter your day of birth";
	cin>>bd;
	clrscr();

	int totalCDays = calcTotalDays(cy,cm,cd);
	int totalBDays = calcTotalDays(by,bm,bd);
	int ageDays = totalCDays - totalBDays;


	cout<<calcTotalDays(cy,cm,cd) - calcTotalDays(by,bm,bd);
	int ageYear = ageDays / 365;
	ageDays %= 365;
	int ageMonths = ageDays / 30;
	ageDays %= 12;
	cout<<"age is "<<endl;
	cout<<ageYear<<endl;
	cout<<ageMonths<<endl;
	cout<<ageDays;
	getch();
}
Spread the love

Related Posts