# Calculate Days Between 2 Months

This is a discussion on Calculate Days Between 2 Months within the C Programming forums, part of the General Programming Boards category; Hi, i need help in writing a simple program which can calculate the number of days between 2 months, for ...

1. ## Calculate Days Between 2 Months

Hi,

i need help in writing a simple program which can calculate the number of days between 2 months, for the year 2010.

E.g. if the user enters start month as 1, and end month as 3, the program should be able to calculate the numbers of days From 1st January To 31 March.

Thanks

2. What have you done so far?

3. While waiting for you to give it your best shot before posting code, here's a couple of general hints to get you started...

1) you're going to need a data base with the number of days in each month.
2) your software is going to have to account for leap years.
3) You will also most likely want the day of the month for your star and end periods.

It's a fairly simple calculation so I'm betting you can figure that out on your own.

4. I have been trying various things.

So far i can get the user to enter a start month and a end month. I am not sure of what to do next.

Thanks

5. Well... you need to know when to start calculating and when to stop.... so what do you think?

6. Need to start calculating once the user has entered both months.

I'm thinking i need an array to say how many days are in each month,

Code:
`int NumDaysInEachMonth[] ={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};`
But i don't understand how i will say that January has 31 days, February has 28 days.
I will then add the 2 values together, then display that value.

7. You do need an array, but like this....

Code:
`int NumDaysInEachMonth[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};`
For the days calculation...
Code:
```// user enters 3 (March)
int StartMonth;

Days = NumDaysInEachMonth[StartMonth];```
You're on your own for the rest.
But here's a hint... there are only 31 days from January 1 to February 1...

8. Hi, thanks i can get it to add the values entered for start date and end date.

But the problem is, that at the moment it only adds 13 and 13, as the program keeps asking for start and end months until 13 is entered (as 13 is the one for exit), how can i get the program to stop asking for a start / end date after it has been entered once?

Thanks

9. Hi, I've got it working I think. Going to test it properly
Thanks

10. The program i've got working adds the number of days for 2 months, e.g Jan 31 and Oct 31 = 62. I need the program to calculate days for all the months in between also, so Jan + Feb + March ... all the way to the end of october. How would i do this?
Thanks

The program i've got working adds the number of days for 2 months, e.g Jan 31 and Oct 31 = 62. I need the program to calculate days for all the months in between also, so Jan + Feb + March ... all the way to the end of october. How would i do this?
Thanks
Think "loops"....

12. yea thats what ive been trying, but can't figure it out.

i put month 1 and month 2 values into i and j. then try and use the loop to add all values from i, till the value of j?

Code:
```i=month1;
j=month2;

for (counter=0; counter < 13; counter++)
{
numdaysbetween2months = monthdays[i] + monthdays[counter];
}```
thanks

yea thats what ive been trying, but can't figure it out.

i put month 1 and month 2 values into i and j. then try and use the loop to add all values from i, till the value of j?

Code:
`for (counter=START; counter < END; counter++)`

14. could you please explain, i don't understand
thanks

15. Like CommonTater mentioned:
int NumDaysInEachMonth[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
You get the start month assign it to a variable let say int startMonth, and end month as int endMonth. If 1-12 is the input of the user from January to December, then:
for(i = startMonth; i<=endMonth; i++)
{
totalDay = totalDay+NumDaysInEachMonth[i];
}
note that your totalDay should be set to 0 by default.

Page 1 of 2 12 Last