we get month for (month=days/30;) years for ( years=month/12;) but we also get days for(( days=days%30, why it happend explain ??))

Recommended Answers

All 6 Replies

It's wrong. It's an approximation that assumes every month has 30 days.

  1. 30 days has September, April, June, and November.
  2. All the rest have 31 except February which has 28 unless it is a leap year, in which case it has 29.
  3. Leap years are those where year % 4 == 0 unless year % 100 == 0 but it's still a leap year when year % 400 == 0.

Or in more simple terms (from Wikipedia):

if (year is not exactly divisible by 4) then (it is a common year)
else
if (year is not exactly divisible by 100) then (it is a leap year)
else
if (year is not exactly divisible by 400) then (it is a common year)
else (it is a leap year)

Of course, this only applies to Gregorian calendars, which we use today... :-)

And BTW, years != month/12, unless you are talking about the total number of months beyond or == 12, so you should say:

(months~=total_days/30;), (years=total_months/12;), (day_of_the_month~=total_days%30). Remember the approximate symbol (~). Approximations are good, if you remember to note them, otherwise you are just plain wrong!

days=days%30 ..here to find out the days ,why we use the symbol % here ??

//Hint:

//Do you know what modular arithmetic (here, division) means?

//If you have these next two integers stored in 'a' and 'b':

int a = 23;
int b = 10;

// Do you know what values the following x, y would hold?
int x = a / b ;
int y = a % b ;

// if not,
// study (do a web search on)
// 'integer division'
// and also
// 'modular division'.

Per David W's post, modular arithmetic returns the remainer of a division operation, so 4%2 is the remainder of 4/2 which is 0. If there are 30 days in a month, then if it is the 15th of the month, the remainder of 15/30 is 15, so 15%30 == 15. If we are using the day of the year to determine the month, and day of the month, assuming all months have 30 days, then if the day is 250, the month is 250/30 == 8 with a remainder of 10. So 250%30 == 10. IE, the date would be the 10th of the 9th month (September 8th) as the 8th month (August) is complete.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.