Results 1 to 5 of 5

Thread: How to write a program to determine if year is leap year or not

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to write a program to determine if year is leap year or not

    Hi,
    I am last year B.Sc.IT student. I just started learning java language. In our last tutorial our sir has given us one program like write a program to determine if year is leap year or not. I tried various method but I unable to write that program. Please if you know any logic behind it then give it to me.
    thank you.
    Last edited by Luis-Fernando; 30-11-2009 at 05:06 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to write a program to determine if year is leap year or not

    Here is the program.

    Code:
    import java.util.*;
    import java.io.*;
    
    public class LeapYear{
      public static void main(String[] args) throws IOException{
        BufferedReader inter = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter year : ");
        int y;
        try{
          y = Integer.parseInt(in.readLine());
          if(y < 1900 || y > 2100){
            System.out.print("enter year that is less than 2101 and greater than 1899.");
            System.exit(0);
          }
          GregorianCalendar c = new GregorianCalendar();
          if(c.isLeapYear(y))
            System.out.print("Enter year is leap year.");
          else
            System.out.print("Enter year is not leap year.");
        }
        catch(NumberFormatException ne){
          System.out.print(ne.getMessage() + " is not a proper number.");
          System.out.println("Please proper number.");
          System.exit(0);
        }
      }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to write a program to determine if year is leap year or not

    I think this is one of the most simplest progran I have written.


    Code:
    public class LeapYear {
    
    public static void main(String[] args) {
     
    int y = 2004;
    
    if(y % 4 == 0)
    
    System.out.println("Year " + y + " is Leap year");
    
    else
    
    System.out.println("Year " +  + " is not Leap year");
    }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: How to write a program to determine if year is leap year or not

    Use following code to write ptogram.

    Code:
    GregorianCalendar cal1 = new GregorianCalendar();
        boolean opt = cal1.isLeapYear(1998); 
        opt = cal1.isLeapYear(2000);        
        opt = cal1.isLeapYear(0);

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to write a program to determine if year is leap year or not

    Code:
    import java.text.ParseException;
    
    public class MainClass {
    
      public static void main(String[] args) throws ParseException {
        System.out.println(isLeapYear(2000));
      }
    
      public static boolean isLeapYear(int y) {
    
        if (y < 0) {
          return false;
        }
    
        if (y % 400 == 0) {
          return true;
        } else if (y % 100 == 0) {
          return false;
        } else if (y % 4 == 0) {
          return true;
        } else {
          return false;
        }
      }

Similar Threads

  1. Which are the best HTC phones for this year?
    By Oppilan in forum Portable Devices
    Replies: 4
    Last Post: 11-06-2012, 01:29 PM
  2. Replies: 1
    Last Post: 17-01-2012, 07:11 PM
  3. My first year Doing IT in College
    By Zucks in forum Education Career and Job Discussions
    Replies: 2
    Last Post: 07-03-2011, 12:44 AM
  4. How to get out of 2 year contract with AT&T
    By Zacharia in forum Portable Devices
    Replies: 3
    Last Post: 24-08-2009, 06:10 PM
  5. How to use PHP get year in firefox ?
    By Seraphim in forum Software Development
    Replies: 2
    Last Post: 20-05-2009, 01:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,728,546,929.53395 seconds with 17 queries