Saturday, 19 October 2013

How to check if year is a leap year in java and C#

Leave a Comment
Hye there, its quite complicated to check in if else statement the year is a leap year or not.   Today i will show the example how to check if the year is a leap year or not.

Leap Year
  • A Normal day for a year is 365 days
  • Leap year have 366 days (29 days in february)
How to know if the year is  leap year
  • Leap year can be any year that can be divided by 4 without any balance.
WHY leap year occur
  • This is because according to research, The earth rotate about 365.242375
  • but in one year only have 365 days.
  • So something has to be done to handle the remaining 0.242375 balance. that is why the leap year occur only once in 4 years.

The example is pretty easy to follow.

The Java Application

package leapyear;

import java.util.Scanner;

public class LeapYear {
    
    public static void main(String[] args) {
        String Cont = "Y";
        Scanner input = new Scanner(System.in);
        LeapYear lYear = new LeapYear();
        
        System.out.println("---------------------------");
        System.out.println("Program to check if year is leap year or not");
        System.out.println("---------------------------");
        
        while(Cont.equalsIgnoreCase("Y")){
            
            System.out.println("Please Enter the year to check in format \"YYYY\"");
            String year = "";
            while(year.length() != 4){
                year = input.nextLine();
                if(year.length() != 4){
                    System.out.println("Please Enter the year in format \"YYYY\"");
                }               
            }
            System.out.println("Year " + year + " is a leap year : " + lYear.checkIfYearIsLeapYear(Integer.parseInt(year)));
            
            System.out.println("Do you want to check abother year?(Y,N)");
            Cont = input.nextLine();
            
        }
    }
    
    public boolean checkIfYearIsLeapYear(int iYear){
        if ((iYear % 400 == 0) || ((iYear % 4 == 0) && (iYear % 100 != 0))) {
            return true;
        } else {
            return false;
        }
    }
}
 

The C# Program

using System;
using System.Text;

namespace Check_LeapYear
{
    class Program
    {
        static void Main(string[] args)
        {
            String Cont = "Y";

            Program lYear = new Program();

            Console.WriteLine("---------------------------");
            Console.WriteLine("Program to check if year is leap year or not");
            Console.WriteLine("---------------------------");

            while (Cont.ToUpper().Equals("Y"))
            {

                Console.WriteLine("Please Enter the year to check in format \"YYYY\"");
                String year = "";
                while (year.Length != 4)
                {
                    year = Console.ReadLine();
                    if (year.Length != 4)
                    {
                        Console.WriteLine("Please Enter the year in format \"YYYY\"");
                    }
                }
                Console.WriteLine("Year " + year + " is a leap year : " + lYear.checkIfYearIsLeapYear(Convert.ToInt32(year)));

                Console.WriteLine("Do you want to check abother year?(Y,N)");
                Cont = Console.ReadLine();

            }
        }

        public bool checkIfYearIsLeapYear(int iYear)
        {
            if ((iYear % 400 == 0) || ((iYear % 4 == 0) && (iYear % 100 != 0)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
} 

The Output

---------------------------
Program to check if year is leap year or not
---------------------------
Please Enter the year to check in format "YYYY"
1989
Year 1989 is a leap year : false
Do you want to check abother year?(Y,N)
Y
Please Enter the year to check in format "YYYY"
89
Please Enter the year in format "YYYY"
2011
Year 2011 is a leap year : false
Do you want to check abother year?(Y,N)
Y
Please Enter the year to check in format "YYYY"
1996
Year 1996 is a leap year : true
Do you want to check abother year?(Y,N)
N


C# output

















References



By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.