Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: I need help writing this program

1. ## I need help writing this program

In general terms the algorithm for calculating a leap year is as follows...
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
Write a program that asks the user how many years they want to check.
For that number of times the program accepts as data a year. The program will then determine if the year is a leap year or not. The program will keep doing this until the correct number of years is checked.
Display the percentage of input years that are leap years, as well as the percentage that are not leap years. Confirm these add to 100.
using formatting as described in the text, make a table of your output. Real numbers should have 2 decimal places, and decimal points should line up
can you put the comments on each code for what they do. Thank you if you can help me

2. ## Re: I need help writing this program

Hello kev2000, welcome to the forums.

Have you started any of this assignment yet? Please post what you have so far and we can guide you in the right direction.

3. ## Re: I need help writing this program

I haven't started yet, I don't know where to start. It will be nice if you can help me understand.

4. ## Re: I need help writing this program

Originally Posted by kev2000
I haven't started yet, I don't know where to start. It will be nice if you can help me understand.

It will read in the year submitted by the user in the console. Then it will work out if the year is divided by 4. See if you can work out the rest...

```import java.util.Scanner;

public class Kev2000 {

/**
* JavaProgrammingForums.com
*/
private static int year;
private static boolean isLeapYear = false;

public void divisibleBy4() {

String myYear = Integer.toString(year);
myYear = myYear.substring(2, 4);
int myYearInt = Integer.parseInt(myYear);

for (int a = 0; a < myYearInt; a++) {
int calc = (4 * a);
if (calc == myYearInt) {
System.out.println(year + " is divisible by 4");
//Next part of the calculations needed here
break;
}

}

isLeapYear = false;

}

public static void main(String[] args) {

Kev2000 k = new Kev2000();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a year: ");
year = sc.nextInt();
k.divisibleBy4();
}

}```

5. ## Re: I need help writing this program

Originally Posted by JavaPF

It will read in the year submitted by the user in the console. Then it will work out if the year is divided by 4. See if you can work out the rest...

```import java.util.Scanner;

public class Kev2000 {

/**
* JavaProgrammingForums.com
*/
private static int year;
private static boolean isLeapYear = false;

public void divisibleBy4() {

String myYear = Integer.toString(year);
myYear = myYear.substring(2, 4);
int myYearInt = Integer.parseInt(myYear);

for (int a = 0; a < myYearInt; a++) {
int calc = (4 * a);
if (calc == myYearInt) {
System.out.println(year + " is divisible by 4");
//Next part of the calculations needed here
break;
}

}

isLeapYear = false;

}

public static void main(String[] args) {

Kev2000 k = new Kev2000();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a year: ");
year = sc.nextInt();
k.divisibleBy4();
}

}```
A quick suggestion: wouldn't it be simplier to use mod function?

boolean divBy4 = myYearInt % 4 == 0

6. ## Re: I need help writing this program

Originally Posted by Dalisra
A quick suggestion: wouldn't it be simplier to use mod function?

boolean divBy4 = myYearInt % 4 == 0
It would indeed be a lot simpler to use that function! Thank you Dalisra.