Write the program in c++ Write a program that prints the day number of the year,
ID: 3793103 • Letter: W
Question
Write the program in c++
Write a program that prints the day number of the year, given the date in the form month-day-year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4, but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400.Explanation / Answer
// C++ code
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
int date,month,year;
int totalDays=0;
printf("Enter date mm-dd-yyyy: ");
scanf("%d-%d-%d",&month,&date,&year);
while(month--)
{
switch(month)
{
case 11:totalDays = totalDays + 3 0;
break;
case 10:totalDays = totalDays + 31;
break;
case 9:totalDays = totalDays + 30;
break;
case 8:totalDays = totalDays + 31;
break;
case 7:totalDays = totalDays + 31;
break;
case 6:totalDays = totalDays + 30;
break;
case 5:totalDays = totalDays + 31;
break;
case 4:totalDays = totalDays + 30;
break;
case 3:totalDays = totalDays + 31;
break;
case 2:if((!(year%4))&&(year%100))
totalDays = totalDays + 29;
else if(!(year%400))
totalDays = totalDays + 29;
else
totalDays = totalDays + 28;
break;
case 1:totalDays = totalDays + 31;
break;
case 0:break;
}
}
cout << "totalDays: " << (totalDays+date) << endl;
return 0;
}
/*
output:
Enter date mm-dd-yyyy: 12-25-2006
totalDays: 359
Enter date mm-dd-yyyy: 12-31-1992
totalDays: 366
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.