The following is my code I need to do these steps... using c++ 1. Verify the mon
ID: 3716050 • Letter: T
Question
The following is my code I need to do these steps... using c++
1. Verify the month range is correct. If not, tell the user.
2. Verify the day range of the month is correct. If not tell the user.
3. Use the function that will determine if the year entered is a leap year.
#include "std_lib_facilities.h"
a = (14 - Month) / 12
m = Month + 12a - 3
y = Year + 4800 - a;
nDate = Day + ((153 * m + 2) / 5) + 365 * y + y / 4 - y / 100 + y / 400 - 32045
* /
#include <iostream>
using namespace std;
//function definition
int serial_julian_date(int Month, int Day, int Year)
{
double a = (double)(14 - Month) / 12;
int m = Month + 12 * a - 3;
int y = Year + 4800 - a;
double nDate =
Day + ((double)(153 * m + 2) / 5) +
365 * y + (double)y / 4 - (double)y / 100 + (double)y / 400 - 32045;
return (int)nDate;
}
//main function
int main()
{
cout << "Enter the date in MM DD YYYY format (seperated by spaces)" << endl;
//declaring necessary variables
int Month, Day, Year;
//taking inputs
cin >> Month >> Day >> Year;
//converting
int d = serial_julian_date(Month, Day, Year);
//display result
cout << "Serial Julian date is :";
cout << endl << d << endl;
return 0;
Explanation / Answer
I write this code to enter and check validity of a date as per my knowledge. It return 1 if date is valid and 0 if invalid.
int vaildDate()
{
int days,month,num,dd,mm,yy;
do{
cout<<"Enter Date: ";
cin>> dd;
}while (dd==1&&dd==31);
do{
cout<<"Enter Month: ";
cin>> mm;
}while (mm==1&&mm==12);
do{
cout<<"Enter Year: ";
cin>> yy;
if (yy%4 ==0)
Cout<<"year is leap year";
}while (yy==0&&yy==num);
// Function to check date according to month
switch (mm){
case 1: case 3: case 5: case 7:case 8: case 10: case 12 : days=31;
break;
case 4: case 6: case9: case 11 : days=30;
break;
case 2 : if (yy%4==0)days=29;
else
days=28;
}
// Function to check the validity of check
if (dd>=1&&dd<=days&&mm>=1&&mm<=month)
{cout<<" Date is Valid!! ";
return 1;
}
else
{
cout<< "Sorry!!Date is Invalid ";
return 0;}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.