0% found this document useful (0 votes)
9 views2 pages

Notes 20240919203702

This document contains a C++ program that calculates the total number of days a person has lived based on their birth date. It includes functions to determine leap years, the number of days in a month, and the main logic to compute the days lived. The program prompts the user for their birth date and outputs the total days lived.

Uploaded by

aswinpramod082
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Notes 20240919203702

This document contains a C++ program that calculates the total number of days a person has lived based on their birth date. It includes functions to determine leap years, the number of days in a month, and the main logic to compute the days lived. The program prompts the user for their birth date and outputs the total days lived.

Uploaded by

aswinpramod082
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

09.

19 8:36 PM
#include <iostream>
#include <ctime> // for time functions

// Function to check if a year is a leap year


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

// Function to calculate the total number of days in a month


int daysInMonth(int month, int year) {
switch(month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return 0;
}
}

// Function to calculate the total number of days from birth date to today
int calculateDaysLived(int birthDay, int birthMonth, int birthYear) {
// Get current date
time_t now = time(0);
tm *ltm = localtime(&now);

int currentDay = ltm->tm_mday;


int currentMonth = 1 + ltm->tm_mon; // tm_mon starts from 0 for January
int currentYear = 1900 + ltm->tm_year; // tm_year is years since 1900

int daysLived = 0;

// Days from birth date to the end of the birth year


for (int m = birthMonth; m <= 12; ++m) {
if (m == birthMonth) {
daysLived += daysInMonth(m, birthYear) - birthDay;
} else {
daysLived += daysInMonth(m, birthYear);
}
}

// Days from the start of the current year to today's date


for (int m = 1; m < currentMonth; ++m) {
daysLived += daysInMonth(m, currentYear);
}
daysLived += currentDay;

// Days for the years in between


for (int y = birthYear + 1; y < currentYear; ++y) {
daysLived += isLeapYear(y) ? 366 : 365;
}

return daysLived;
}

int main() {
int birthDay, birthMonth, birthYear;

// Input birth date


std::cout << "Enter your birth date (day month year): ";
std::cin >> birthDay >> birthMonth >> birthYear;

int daysLived = calculateDaysLived(birthDay, birthMonth, birthYear);

std::cout << "You have lived " << daysLived << " days." << std::endl;

return 0;
}

You might also like