USING C++ Write a program that calculates the number of days elapsed till a give
ID: 3882165 • Letter: U
Question
USING C++
Write a program that calculates the number of days elapsed till a given date since the beginning of the year. Your program should prompt the user to input the year in yyyy, month in mm, and day in dd format. Your program should be fool proof which means it should check for valicd values for year, month, and date. You need to consider that leap year has 29 days in February Your output should display the number of days elapsed till the given date yyyy/mm/dd Sample input and output 2016 03 01 61 days 2015 03 01 60 days 2015 05 32 Invalid date Please submit your pseudocode (in a separate file) and your source code to eLearningExplanation / Answer
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
int year = 2018, month = 02, date = 02;
int new_year, new_month, new_date, days, i;
int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main()
{
cout<<" Enter Year Month Date:";
cin>>new_year >>new_month >>new_date;
if(new_year > 9999 || new_year <= 0)
cout<<" Invalid entry";
if(new_month > 12 || new_month <= 0)
cout<<" Invalid entry";
if(new_date > 31 || new_date <= 0)
cout<<" Invalid entry";
if(year == new_year)
{
if(month == new_month)
days = new_date - date;
else
{
for(i = month; i < new_month - 1; i++)
days = days + month_days[i];
days = days + month_days[month - 1] - date + new_date;
}
}
else
{
for(i = 0; i < new_month - 1; i++)
days = days + month_days[i];
for(i = month; i < 12; i++)
days = days + month_days[i];
if(new_year - year >= 0)
{
days = days + (new_year - year - 1) * 365 + month_days[month - 1] - date + new_date;
}
}
if((new_month <= 12) && (new_date <= 31))
{
cout<<" Given date: " <<year <<" " <<month <<" "<<date;
cout<<" New date: " <<new_year <<" " <<new_month <<" " <<new_date;
cout<<" " <<days << " days";
}
return 0;
}
OUTPUT
Enter Year Month Date:2019 02 32
Invalid entry
Enter Year Month Date:2019 02 20
Given date: 2018 2 2
New date: 2019 2 20
383 days
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.