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

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

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.
Reply With Quote
  #2  
Old 30-11-2009
Member
 
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);
    }
  }
}
Reply With Quote
  #3  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
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
Member
 
Join Date: May 2008
Posts: 2,293
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
Member
 
Join Date: Oct 2005
Posts: 2,389
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
Tags: , , , ,



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
Which are the best HTC phones for this year? Oppilan Portable Devices 4 11-06-2012 01:29 PM
Final year student of computer engineering needs help to run program on limited Account m26.Sumit Software Development 1 17-01-2012 07:11 PM
My first year Doing IT in College Zucks Education Career and Job Discussions 2 07-03-2011 12:44 AM
How to get out of 2 year contract with AT&T Zacharia Portable Devices 3 24-08-2009 06:10 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:15 AM.