Home » .Net

Find leap years form array of integers using C# program

In this C# program, we will learn, how to find out leap years from given list of leap years? We will create an array that contains number of year.

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:

  1. If given year is divisible by 4 and not divisible by 100 then it will be leap year.
  2. If given year is divisible by 4 and divisible by 100 but not divisible by 400 then it will not be a leap year.
  3. If given year is divisible by 4 and divisible by 100 and also divisible by 400 then it will be a leap year.

For example we have list of integers:

1600 1604 1605 1900 2000

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

Consider the example:

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.