0% found this document useful (0 votes)
4 views

6 program solution

This C program calculates the total number of days a user has lived based on their date of birth and today's date. It also computes the total minutes spent sleeping and eating, as well as the years spent reading. The program includes functions to determine the number of days in each month and accounts for leap years.

Uploaded by

Sunita Jeevangi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

6 program solution

This C program calculates the total number of days a user has lived based on their date of birth and today's date. It also computes the total minutes spent sleeping and eating, as well as the years spent reading. The program includes functions to determine the number of days in each month and accounts for leap years.

Uploaded by

Sunita Jeevangi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>

int main() {
int dob_day, dob_month, dob_year;
int today_day, today_month, today_year;

// Read user's date of birth


printf("Enter your date of birth (dd-mm-yyyy): ");
scanf("%d-%d-%d", &dob_day, &dob_month, &dob_year);

// Read today's date


printf("Enter today's date (dd-mm-yyyy): ");
scanf("%d-%d-%d", &today_day, &today_month, &today_year);

// Calculate the total number of days lived


int total_days = 0;

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


int days_in_month(int month, int year) {
if (month == 2) {
// Check for leap year
return (year % 4 == 0 && (year % 100 != 0 || year % 400 ==
0)) ? 29 : 28;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
return 31;
}

// Calculate days from the date of birth to the current date


for (int year = dob_year; year < today_year; year++) {
total_days += 365 + (year % 4 == 0 && (year % 100 != 0 || year
% 400 == 0));
}

// Add days for the months in the current year


for (int month = 1; month < today_month; month++) {
total_days += days_in_month(month, today_year);
}

// Add days for the current month


total_days += today_day;

// Subtract days for the months before the birth month in the birth
year
for (int month = 1; month < dob_month; month++) {
total_days -= days_in_month(month, dob_year);
}

// Subtract days for the days before the birth day in the birth month
total_days -= dob_day;

// Calculate time spent sleeping and eating


int sleeping_hours_per_day = 8;
int eating_hours_per_day = 1.5;
int total_sleeping_minutes = total_days * sleeping_hours_per_day
* 60;
int total_eating_minutes = total_days * eating_hours_per_day * 60;

// Calculate years spent reading


int reading_hours_per_day = 12;
int total_reading_hours = total_days * reading_hours_per_day;
int total_reading_years = total_reading_hours / (24 * 365); //
Convert hours to years

// Output results
printf("Total minutes spent sleeping: %d\n",
total_sleeping_minutes);
printf("Total minutes spent eating: %d\n", total_eating_minutes);
printf("Total years spent reading: %d\n", total_reading_years);

return 0;
}

You might also like