1.

Solve : C# finding a whole number?

Answer»

Hi I was hoping someone might be able to help me.

I have to write a program in C#. part of that program is finding out whether a year is a leap year or not. Now I know that if you divide the year by 4 and it comes BACK with a whole integer number that it is a leap year.

My question is how can I CHECK that the number it comes back with is a whole integer number?Quote from: -kat- on February 21, 2010, 08:47:10 AM

Now I know that if you divide the year by 4 and it comes back with a whole integer number that it is a leap year.
First- this is actually not the entire rule.

A year is a leap year if it's evenly divisible by 4 but not by 100


if (year modulo 4 is 0) and (year modulo 100 is not 0) or (year modulo 400 is 0)
then is_leap_year
else
not_leap_year


Anyway- the answer to your problem is the C# MODULUS operator, %: for example, this function would DETERMINE if a year was a leapyear:


Code: [Select]private bool IsLeap(int year)
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
return true;
else
return FALSE;

}
However- a better solution is to simply use the .NET framework. System.DateTime.IsLeapYear(int) will tell you if a specified year is a leap-year.Quote from: BC_Programmer on February 21, 2010, 09:04:05 AM
However- a better solution is to simply use the .NET framework. System.DateTime.IsLeapYear(int) will tell you if a specified year is a leap-year.

Better - but not so much math practice involved... the teacher of the C# class might give -kat-'s homework a low mark if he or she did that!
Quote from: Salmon Trout on February 21, 2010, 09:23:38 AM
Better - but not so much math practice involved... the teacher of the C# class might give -kat-'s homework a low mark if he or she did that!



Yes, that's why I included the other stuff .


Discussion

No Comment Found