#include <iostream>
using namespace std;
void mthchange(int day) {
int mday = 0, mth = 0;
int arr[12] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
while (mday <= day) {
mday += arr[mth];
mth++;
}
cout <<mth << "Month" << day - mday << "Day" << endl;
}
int main (void) {
int i;
cin >> i;
mthchange(i);
return 0;
}
I did it like this. If I enter 12, it says February 19th.
c++
#include <iostream>
using namespace std;
void mthchange(int day) {
int mday = day, mth = 0;
int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
For (mth = 0; mday > arr[mth]; mth = (mth + 1) % 12) //Exit if the number of days in the month is less than or equal to the number of days
{
mday -= arr[mth];
}
cout << mth + 1 << "Month" << mday << "Day" << endl;
}
int main(void) {
int i;
cin >> i;
mthchange(i);
return 0;
}
If you enter more than 366, you will get the value you want.
Leap year treatment is excluded.
If you want to keep the code to a minimum, Let's just change the conditions of the while statement.
while (mday + arr[mth] < day)
I think this will work, but I don't know exactly because I haven't tried it.
© 2024 OneMinuteCode. All rights reserved.