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.
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();
}
}
Comments
Leave a comment