techhipbettruvabetnorabahisbahis forumutaraftarium24eduseduedueduseduedusedueduedusedu Java Program To Check Leap Year Or Not - 4 Ways
Latest :

Java Program To Check Leap Year Or Not – 4 Ways

Java Program to find if a year is a leap or not. Here, we see the various methods to find out whether a year is Leap or not in Java In 5 different ways. Soon Compiler is added so that you can execute the program yourself, along with suitable examples and sample outputs. The methods used in this case are:

    • Using Ternary Operator.
    • Using Static Method.
    • Using Command Line Arguments.
  • Using Function.
  • Using If-Else.

A LEAP YEAR, also known as an intercalary year or a bissextile year is a calendar year containing 366 days, as compared to the regular 365 days a year.

The extra day is added to balance out the drift between an astronomical year and a seasonal year. The Earth’s revolution generally takes 365.25 days approx to complete. The extra day is added every four years to bridge that extra time period. Here’s an interesting link that explains why years 1800, 1900, 2100 are not Leap years, even though they are divisible by 4.

Using Ternary Operator

1) The ternary operator syntax is  “if (condition)? value1:value2;”, if the condition is true then it returns value1, otherwise it returns value2.

2) We are calculating the given year is a leap year or not. A year divisible by 400 (or) a year divisible by 4 and not divisible by 100 is a leap year.

3) Read the year value using scanner object sc.nextLong and store it in the variable y.

In given example y=1948, y!=0, it is divisible by 400 so c=1 return to a, a is initialized as a=1. Then it prints 1948 is a leap year.

Output:

Using Static Method

1) In this program leapOrNot(long year) is the static method which finds the given year is a leap year or not. leapOrNot(long year) method calls at the main method of the class Leapyear. Then that static method starts the execution.

a) If the year divisible by 400 then it prints the given year is leap(or) If the given year divisible by 4 and not divisible by 100 then it prints the given year is a leap.

b) Otherwise, it prints “year is not a leap year”.

Output:

Using Command Line Arguments

1) We can read command line arguments from “String args[]” of the main method.

2) Convert the year which is in the string format at arg[0], in to int using year=Integer.parseInt(arg[0]);. Integer is the wrapper class which converts the string into int data type.

a) If the year divisible by 400 then it prints the given year is leap or if the given year divisible by 4 and not divisible by 100 then it prints the given year is a leap.

b) Otherwise, it prints “year is not a leap year”.

Output:

Java Leap Year Or Not – Using Function

1) The leap(int year) function returns c=1 if the given year is a)divisible by 400 or b) divisible by 4 and not divisible by 100.

2) The function calls at the main method if year!=0, then it returns the c value and c value initialized to the variable “a” if a=1 then prints  “is a leap year” otherwise prints “not a leap year”.

Output:

Using If-Else

1) We are using if else condition to find the given year is a leap year or not.

2) The 1st if condition checks the given year is equal to zero or not, if not

a) If given year divisible by 400 it prints  “is a leap year”, if condition false then come to else part.

b) If given year divisible by 100, then inner if condition checks the year is divisible by 4 or not if true then prints “is a leap year” otherwise prints “is not a leap year. If condition if(year%100!=0) is false then it prints “is not a leap year”.

Output:

More Java Programs:

techhipbettruvabetnorabahisbahis forumutaraftarium24edueduedusedusedueduedueduseduedus
x

Check Also

Reverse A Number In Java – 4 Simple Ways | Programs

Reverse A Number In Java – We have discussed the various methods to reverse a ...

betwoonhacklink satın albets10 girişakcebet uyeliksupertotobet1xbet girişmelbet girişbetwinnerpinup uyelikbtcbahis girişmostbet girişbetwinner girişxslotskyloft uyelikmegapari uyeliktipobetgrandpashabetradissonbetmariobett.mebetandreasenbetcasivalmisbahismyhitbetonbahistipobetbahsinevavepasgolgrandpashabetmarsbahispusulabetcasibomcasibom girişcasibom güncel girişcasibomcasibom girişdeneme bonusu veren sitelerhttps://mobilebarhawaii.com/ballettea tropazmegabahis güncel girişmatadorbetbets10betturkeycasibomkingroyalbets10 girişbets10bets10maldives casinohacklinkgrandpashabetbetmatik girişmelbetmostbetmegapari üyelikistanbul escortmatadorbetjojobetJojobetjojobetpinco girisonwinonwin girişcasibom güncel girişselcuksportshdcasibomcasibomatakoy escortatakoy escortsisli escortgalatabahisliste paris sportifjeux casino gratuit avec bonusSimsinos Casinojojobet girişCashimashi AvisWunderWinsparier sur les cornersGamdomCasibommadridbet girişatlasbet twitterpinbahisemiratesbettrbetcasibomcratosroyalbetpalacebetcasinokaliprensbetmostbet guncel girisbetandyou guncel girisromabet guncel giris7slots guncel girisbizbet guncel girisrocksroyalbetkazandradeneme bonusu veren sitelergrandpashabetonwinprincessbethttp://nakanakadining.com/betkanyonpinup yeni girismatadorbet yeni girisroketbet yeni girisskyloft yeni girismegapari yeni girissavoybettingcasinomhubligobetmavibetenobahisJojobetpusulabetjetbetonwin girişkingroyal girişarnavutköy escortkingroyalmaltcasinoholiganbet girişsahabetjojobethttps://www.escortbayanlariz.net/beylikdüzü escortfikstürbetmisliwinmarsbahisbets10meritkingsekabet girişvbetsahabet girişholiganbetmeritkingholiganbet girişholiganbet girişjojobetjojobetbets10Jojobet Girişmariobetodeonbetodeonbetbetwoonjojobetsahabetkingroyalbets10 girişjojobetmeritking girişsekabetmeritbetcasibomcasibom girişpinbahiszbahiscasinometropolcasinomaxiarnavutköy escortGebze escort matbetzbahiscasinomaximarsbahismarsbahisbetebetcasibomperabetbakırköy escortmatbet girişbelugabahis twittersüperbetin güncel girişjojobetpusulabetMavibetOnwinbets10 girişcasibomextrabetcasibomdeneme bonusu veren sitelerholiganbet girişbetturkey girişbetturkeyhalkali escortmobilbahisasyabahiscasibom güncel girişsahabetjojobetjojobetdumanbetmadridbetnakitbahisultrabetultrabetotobetkulisbetfixbetcratosslotbetturkeykralbetholiganbetmatbetcasibom girişjojobet girişpadişahbetbets10 girişbets10onwinselçuksportsSekabetholiganbethttps://www.gvscolombia.com/güvenilir casino siteleri antalya escortbetkanyonjojobetdumanbetganobetganobet girişiptviptvcashbackiptvankara escort bayaninterbahiscasibomgrandpashabetcasival girişmarsbahismatadorbetbetofficeJojobetmaldives betcasibom girişmeritkingbetciojojobet girişgrandpashabet girişbahiscasino girişmegabahis güncel girişbetorderBankobetBetordercasibom 811 giriştaraftarium24holiganbetholiganbetholiganbetjojobetzbahisGrandpashabet güncel girişcasibomjojobetonwinextrabetmatbetextrabet girişmatbet girişbetwoonmarsbahisbets10TipobetpusulabetSekabetcasibomesenyurt anal escortrus escortholiganbet girişGrandpashabetholiganbetcasinomaxicasibomgüvenilir casino siteleri grandpashabetİstanbul Escortbetciohttps://www.gvscolombia.com/marsbahisbetciodeneme bonusu veren sitelerotobetcasinomaximarsbahis girişstarzbetjojobetjojobet girişmrbahismrbahisJojobetsahabetjojobetjojobet girişgooglecasibomkucukcekmece escortsekabetsahabetrestbetpinbahisonwinmeritbetmavibetmatbetmatadorbetmaltcasinokingroyalimajbetholiganbetgrandpashabetizmir escorttexas sweepstakesflorida sweepstakessweepstakes sportsbooksweepslotsbetgaranti girişbetgarantibankobet girişPusulabet güncel girişmobilbahisbetciorestbetcasibomgrandpashabetmatadorbetmaltcasinoholiganbetmarsbahisonwinsahabetsekabetmatbetimajbetsekabet girişvaycasinotuzla escort bayancanlı bahiscasibomimajbetstarzbetmobil ödeme bozdurmajojobet