Q:

Find leap years form array of integers using C# program

belongs to collection: C# Basic Programs | array programs

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Print all Even numbers from array of integers usin... >>
<< Find negative numbers from array of integers using...