How to check if year is leap year in C#

3 Answers

0 votes
using System;

public class Program
{
	public static void Main(string[] args)
	{
		int year = 2020;

		if (year % 4 == 0) {
			if (year % 100 == 0) {
				if (year % 400 == 0) {
					Console.WriteLine("Leap year");
				}
				else {
					Console.WriteLine("Not a leap year");
				}
			}
			else {
				Console.WriteLine("Leap year");
			}
		}
		else {
			Console.WriteLine("Not a leap year");
		}
	}
}




/*
run:
    
Leap year
    
*/

 



answered Oct 16, 2022 by avibootz
0 votes
using System;

public class Program
{
	public static bool IsLeapYear(int year) {
        if (year % 4 == 0 && year % 100 == 0 && year % 400 == 0)
            return true;
        else if (year % 4 == 0 && year % 100 == 0)
            return false;
        else if (year % 4 == 0)
            return true;
        else
            return false;
    }
    
	public static void Main(string[] args)
	{
		Console.Write(IsLeapYear(2020) ? "Leap Year" : "Not a Leap Year" );
	}
}




/*
run:
    
Leap year
    
*/

 



answered Oct 16, 2022 by avibootz
0 votes
using System;
 
public class Program
{
    public static bool IsLeapYear(int year) {
        return year % 4 == 0 && (year % 100 !=0 || year % 400 == 0);
    }
     
    public static void Main(string[] args)
    {
        Console.Write(IsLeapYear(2020) ? "Leap Year" : "Not a Leap Year" );
    }
}
 
 
 
 
/*
run:
     
Leap year
     
*/

 



answered Oct 16, 2022 by avibootz

Related questions

3 answers 251 views
251 views asked May 23, 2015 by avibootz
2 answers 144 views
144 views asked Oct 16, 2022 by avibootz
3 answers 248 views
3 answers 253 views
1 answer 116 views
3 answers 218 views
...