Question #33529

write a program to input number of days and converts it to month and days?

Expert's answer

#include<iostream>

using namespace std;

int main()

{

int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //the array which contains the amount of days in every //month

int number_of_days;

cout << "Please enter the number of days : ";

cin >> number_of_days;

int number_of_months = 0;

int i = 0;

if (number_of_days < 0) {

cout << "Sorry but the number of days can not be negative.

";

system("pause");

exit(0);

}

int year = 1;

while (true) { // till possible subtract the amount of days in every month from number_of_days

if (i > 11) {

i = 0;

year++;

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { // checking a leap year

month[1] = 29;

}

else {

month[1] = 28;

}

}

if (number_of_days < month[i]) { //stoping the loop when number_of_days less than number of days in current month

break;

}

else {

number_of_months++;

number_of_days = number_of_days - month[i];

}

i++;

}

cout << "The number of months : " << number_of_months << "

";

cout << "The number of days : " << number_of_days << "

";

system("pause");

return 0;

}

using namespace std;

int main()

{

int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //the array which contains the amount of days in every //month

int number_of_days;

cout << "Please enter the number of days : ";

cin >> number_of_days;

int number_of_months = 0;

int i = 0;

if (number_of_days < 0) {

cout << "Sorry but the number of days can not be negative.

";

system("pause");

exit(0);

}

int year = 1;

while (true) { // till possible subtract the amount of days in every month from number_of_days

if (i > 11) {

i = 0;

year++;

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { // checking a leap year

month[1] = 29;

}

else {

month[1] = 28;

}

}

if (number_of_days < month[i]) { //stoping the loop when number_of_days less than number of days in current month

break;

}

else {

number_of_months++;

number_of_days = number_of_days - month[i];

}

i++;

}

cout << "The number of months : " << number_of_months << "

";

cout << "The number of days : " << number_of_days << "

";

system("pause");

return 0;

}

## Comments

## Leave a comment