Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links


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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-11-2009
Member
 
Join Date: Nov 2009
Posts: 50
How to write a program to determine if year is leap year or not

Sponsored Links
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 04:06 PM.
Reply With Quote
  #2  
Old 30-11-2009
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,943
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);
    }
  }
}
Reply With Quote
  #3  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,000
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");
}
}
Reply With Quote
  #4  
Old 30-11-2009
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,278
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);
Reply With Quote
  #5  
Old 30-11-2009
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,381
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;
    }
  }
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to write a program to determine if year is leap year or not"
Thread Thread Starter Forum Replies Last Post
Final year student of computer engineering needs help to run program on limited Account m26.Sumit Software Development 1 17-01-2012 06:11 PM
Will the Serious Sam 3: BFE be the best game of the year? PurviJ Video Games 8 27-11-2011 05:47 PM
My first year Doing IT in College Zucks Education Career and Job Discussions 2 06-03-2011 11:44 PM
How to use PHP get year in firefox ? Seraphim Software Development 2 20-05-2009 01:17 PM


All times are GMT +5.5. The time now is 03:42 PM.