hi

"A leap year is never a good sheep year!"

also might be considered carefully, when solving highly sophisticated problems, what Pope Gregor really did when he introduced his new calendar based on "In omnibus solemnitatibus christianis non ignoramus paschale sacramentum esse principium, (sermo 47)":

In the Gregorian calendar, the current standard calendar in most of the world, most years whose division by 4 equals an integer are leap years. In one leap year, the month of February has 29 days instead of 28. Adding an extra day to the calendar every four years compensates for the fact that a solar year is almost 6 hours longer than 365 days.

However, some exceptions to this rule are required since the duration of a solar year is slightly less than 365.25 days. Years which are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years. For example, 1600 and 2000 were leap years, but 1700, 1800 and 1900 were not. Similarly, 2100, 2200, 2300, 2500, 2600, 2700, 2900, and 3000 will not be leap years, but 2400 and 2800 will be. By this rule, the average number of days per year will be 365 + 1/4 − 1/100 + 1/400 = 365.2425, which is 365 days, 5 hours, 49 minutes, and 12 seconds.

The Gregorian calendar was designed to keep the vernal equinox on or close to March 21, so that the date of Easter (celebrated on the Sunday after the 14th day of the Moon that falls on or after 21 March) remains correct with respect to the vernal equinox.[3] The vernal equinox year is about 365.242374 days long (and increasing), whereas the average year length of the Gregorian calendar is 365.2425.

The marginal difference of 0.000125 days means that in around 8,000 years, the calendar will be about one day behind where it is now. But in 8,000 years, the length of the vernal equinox year will have changed by an amount which cannot be accurately predicted (see below). Therefore, the current Gregorian calendar suffices for practical purposes, and Herschel's correction (making 4000 AD not a leap year) will probably not be necessary.
(taken from wikipedia)

krs, cliff

I like my quote a lot better because its much shorter and doesn't contain so much glbbledygook :)

Hello tesuji,

Thanks for clarification, in that case 365 days/year is no longer a fact, therefore someone that write the program to calculate the leapyear or check for leap year need to take this as cinsideration.

I see your piont, for logical thinking this the way to do it, when we talking about divisible by 4 or by 100 or by 400 in my mind it is

if(yyyy / 4 ==0)

, the (%) is dealing with remainder. Why are we using

if(yyyy % 4 == 0)

instead of the one above, unless we only dealing with the remainder only, now that doesn't make sent?????

if( year % 4 == 0) Most of the time if that is true then year is a leap year. 2001 is NOT a leap year because it is not evenly divisible by 4. 2004 IS a leap year because it is evenly divible by 4.


http://www.dataip.co.uk/Reference/LeapYear.php

>>when we talking about divisible by 4 or by 100 or by 400 in my mind it is
yyyy / 4 == 0 is not what that means. If it was then there would be no leap years since year 4 AD! Think about it: 5 / 4 = 1, 6 / 4 = 1 and 7 / 4 = 1.

The mod operator returns the remainder after division. so 2004 % 4 is the same as 2004/4 with a remainder of 0. Integer division always drops all fractions, so to get the fraction you need to use the mod operator or do floating point (and much slower to boot) division.

To find out if a year is a leap year or not you need to know if the remainder after division is 0 or not. You can't do that with normal integer division such as 2004/4.

I see, like this little program that I wrote to convert integer to binary with remainders of 2:

#include <iostream>

int main()

{
	using namespace std;
	int x = 23;
	
	
	cout << "This program will convert integer number to binary\n";
	cout << "Integer number:23 ",
	cout << "Integer " << x << " = ",
	cout << x / 16 % 2
		 << x / 8 % 2
		 << x / 4 % 2
		 << x / 2 % 2 
		 << x / 1 % 2 << " in binary" << endl;
	cin.get();
	return 0;
}

Hello Ancient Dragon,

Thaks for the tips on (mod) operation, as right now my leap year program when enter no metter what year it alway display it is a leap year, it nerver got pass that piont to check if it is leap year or not.

Please repost entire program because I don't know what changes you may have made since last post.

Sorry here the complete code,

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//Declare the function
bool leapyear(int year);

struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month
	static int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	//int months[12]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	//const unsigned int string_month = 12;
	static string arr[12]={"January ","February ","March ","April ","May ","June","July ","August ","September ","October ","November ","December "};

	//string arr[string_month] = {"January","February","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                          
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
  if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		 {
         cout << "INVALID month: Please try again.\n" << endl;
		 //cin.get();
		 system ("pause");
		 exit (1);
         }
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
  if( dd < 1 || dd > months[mm-1] )// Validate to see if input for day is valid for the month
		 {
         cout << "INVALID day: Please try again.\n" << endl;
		 cin.get();
		 system ("pause");
		 exit (1);
		 }
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();
	year = yyyy;
	cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		  
	if(leapyear(year) && mm == 2 && dd > months[2] - 1)// Check to see if leapyear or not 
	   cout << "Invalid # of days for the month of (" << arr[mm - 1] << ").\n";
	else if( dd > months[2] )
       cout << "Invalid # of days for the month of (" << arr[mm - 1] << ").\n";
	if(leapyear(year)&& mm == 2 &&(yyyy % 4) == 0 && (yyyy % 100 != 0) || (yyyy % 400 == 0))
	  {
	  months[2] = 28;
	  cout << "The year " << yyyy << " is not a leap year.\n";
	  }
    else
	  {
      months[2] = 29;
	  cout << "The year " << yyyy << " is a leap year\n";
	  }			
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;
			
cin.get();
return 0;
}

bool leapyear(int year)
  {
	return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	
  }

Please repost entire program because I don't know what changes you may have made since last post.

line 44: you have changed months[] array so that month 0 does not really exist -- Jan is in month[1]. Therefore you have to change line 44 (and other lines like it) to just use mm, not mm-1. if( dd < 1 || dd > months[mm] ) >>else if( dd > months[2] )
You also have to check if mm == 2 else if( mm == 2 && dd > months[2] ) Or just simply else if( dd > months[mm] ) >>if(leapyear(year)&& mm == 2 &&(yyyy % 4) == 0 && (yyyy % 100 != 0) || (yyyy % 400 == 0))
That is too late because of the previous line shown above. You have to make the adjustment before checking if dd > months[mm].

>> else
>> {
>> months[2] = 29;

line 66: Nope. That's wrong. Just because previous tests failed doesn't mean its a leap year. Actually just the opposite -- if leapyear() fails then its NOT a leap year for months[2] should be 28, not 29.

hi gazoo,

if you want to check whether feb, 29 exists, then why not starting simpler than your code from line # 57 et seq. Maybe this way:

int year = 1800, month= 2, day = 29; // merely a test case

   if (leapyear(year))
   { cout << "yes, it's a bisextile" << endl;
   }
   else
   { cout << "no, not a bisextile" << endl;
      if ( month == 2 && day == 29 )
          cout << "sorry, no bisextile day in " << year << endl;
   }

krs, cliff

hi gazoo,

if you want to check whether feb, 29 exists, then why not starting simpler than your code from line # 57 et seq. Maybe this way:

int year = 1800, month= 2, day = 29; // merely a test case

   if (leapyear(year))
   { cout << "yes, it's a bisextile" << endl;
   }
   else
   { cout << "no, not a bisextile" << endl;
      if ( month == 2 && day == 29 )
          cout << "sorry, no bisextile day in " << year << endl;
   }

krs, cliff

How many ways can you spell bisextile ? :)

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.