Home »
.Net »
C# Programs
Find leap years form array of integers using C# program
Learn, how to find out leap years from given list of leap years?
[Last updated : March 19, 2023]
Finding leap years from an array
Given array of integers ((List of years), and we have to all Leap Years.
To find leap years from an array (list of years), we will traverse array and access each element of array and then check the given conditions, if elements satisfies the given conditions then the number (array element) will be leap year.
The conditions for leap years are:
- If given year is divisible by 4 and not divisible by 100 then it will be leap year.
- If given year is divisible by 4 and divisible by 100 but not divisible by 400 then it will not be a leap year.
- If given year is divisible by 4 and divisible by 100 and also divisible by 400 then it will be a leap year.
Example
Input:
1600 1604 1605 1900 2000
Output:
1600 is a leap year
1604 is a leap year
1605 is not a leap year
1900 is not a leap year
2000 is a leap year
C# program to find leap years from an array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
class Program {
static void Main() {
int i = 0;
int[] arr = new int[5];
Console.WriteLine("Enter years : ");
for (i = 0; i < arr.Length; i++) {
Console.Write("Year[" + (i + 1) + "]: ");
arr[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("List of leap years : ");
for (i = 0; i < arr.Length; i++) {
if ((arr[i] % 4 == 0) && (arr[i] % 100 != 0))
Console.Write(arr[i] + " ");
else if ((arr[i] % 4 == 0) && (arr[i] % 100 == 0) && (arr[i] % 400 == 0))
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}
}
Output
Enter years :
Year[1]: 1600
Year[2]: 1604
Year[3]: 1605
Year[4]: 1900
Year[5]: 2000
List of leap years :
1600 1604 2000
C# Basic Programs »