Answer to Question #327478 in Java | JSP | JSF for afrah

Question #327478

Write a program to find if a year is a leap year or not. We generally assume that if a year number is divisible by 4 it is a leap year. But it is not the only case. A year is a leap year if −

1.   It is evenly divisible by 100

2.   If it is divisible by 100, then it should also be divisible by 400

3.   Except this, all other years evenly divisible by 4 are leap years.

So the leap year algorithm is, given the year number Y,

●    Check if Y is divisible by 4 but not 100, DISPLAY "leap year"

●    Check if Y is divisible by 400, DISPLAY "leap year"

●    Otherwise, DISPLAY "not leap year"

For this program you have to take the input, that is the year number from an input file input.txt that is provided to you. The input file contains multiple input year numbers. Use a while loop to input the year numbers from the input file one at a time and check if that year is a leap year or not. Your output should go in the console.


1
Expert's answer
2022-04-13T14:21:33-0400
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Program1 {
    public static void main(String[] args) throws FileNotFoundException {
        String separator=File.separator;
        String s=separator+"Users"+separator+"Oleg"+separator+"Desktop"+separator+"years.txt";
        File y=new File(s);
        int z=0;
        Scanner scan=new Scanner(y);
        while(scan.hasNextInt()){
            int w=scan.nextInt();
            if(w%4==0){
                System.out.println("leap year");
            }
            else if(w%400==0){
                System.out.println("leap year");
            }
            else{
                System.out.println("not leap year");
            }
        }
        scan.close();

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS