|
| ||||||||||
| Tags: for loop, java, java language, java programming, leap year |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to write a program to determine if year is leap year or not
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. |
|
#2
| ||||
| ||||
| 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
| |||
| |||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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;
}
}
__________________ Grand Theft Auto 4 PC Video Game |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |