Results 1 to 2 of 2

Thread: LYL Leep Year Lister.

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    LYL Leep Year Lister.

    Hi this is a small little console project to list leap years between two different dates.
    Very simple to use enter a starting date and an ending date. Hope you find it us full.

    csharp Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. namespace LYL
    7. {
    8.     class Program
    9.     {
    10.         private static bool IsLeapYear(int year)
    11.         {
    12.             //Check for leap year.
    13.             if ((year % 4 == 0 && !(year % 100 == 0 && !(year % 400 == 0))))
    14.                 return true;
    15.             return false;
    16.         }
    17.  
    18.         static void Main(string[] args)
    19.         {
    20.             int _startY = 0;
    21.             int _endY = 0;
    22.             int I = 0;
    23.  
    24.             Console.WriteLine("+------------------+");
    25.             Console.WriteLine("+ Leap Year Lister +");
    26.             Console.WriteLine("+ By Ben           +");
    27.             Console.WriteLine("+------------------+");
    28.  
    29.             try
    30.             {
    31.                 Console.Write("Enter starting year: ");
    32.                 _startY = int.Parse(Console.ReadLine());
    33.                 Console.Write("Enter ending year: ");
    34.                 _endY = int.Parse(Console.ReadLine());
    35.             }
    36.             catch
    37.             {
    38.                 return;
    39.             }
    40.  
    41.             //Print out years
    42.             for (I = _startY; I < _endY + 1; I++)
    43.             {
    44.                 //Test for leap year.
    45.                 if (IsLeapYear(I))
    46.                 {
    47.                     //Was leap year.
    48.                     Console.WriteLine(I + " is a leap year");
    49.                 }
    50.                 else
    51.                 {
    52.                     //Was not a leap year
    53.                     Console.WriteLine(I + " was not a leap year.");
    54.                 }
    55.             }
    56.         }
    57.     }
    58. }

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: LYL Leep Year Lister.

    I know this isn't code it better, but instead of using a try/catch to check whether a user has input a valid integer, you should really be using .TryParse. If, at any time, you can handle an error (and actually do something about it) without using an exception, you should.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width