SlideShare a Scribd company logo
SAMPLE JAVA PROGRAMS
Comparing Two Numbers This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one .
First of all, name a class "Comparing" and take two variables in this class (ie, a & b). Here we have taken a=5 and b=10, Now we have to find out whether a=b, a>b or b>a.  To find out this apply if and else condition one by one.  Now apply the condition "if (a=b)", if this satisfies then print both are equal. If this doesn't satisfy, then check whether a>b by applying the "else if" condition and print the message "a is greater than b”.  Again this doesn't satisfy then 'else' condition as shown in the example will show that b is greater than a. 
code of the program: class   Comparing{    public static void  main(String[] args) {      int  a=5, b=10;      if  (a == b){        System.out.println("Both are equal");     }      else if (a>b){         System.out.println("a is greater than b");        }      else {          System.out.println("b is greater than a");       }   } }
How to compile & run  Compiling: javac Comparing.java Run: java Comparing Out put: b is greater than a
Program to list all even numbers between two numbers This is a program for listing out all the even numbers between two numbers.
For this first create a class named  AllEvenNum  under the java.io package. Now use the try/catch exception to avoid any kind of input error. After this create a buffer class in which all the input data are stored and modified. Then give message as to &quot;Enter number&quot; in the  System  method.   As we have to find out all the even numbers between 1 and the input number, define an integer variable 'num'.  Now apply  ParseInt  method that parses the string character into decimal integer.  Apply  for  loop in which define an integer i=1 and i<=  num also with an increment operator. Then apply the  if  condition that i/2=0 i.e. to find even numbers which are divided by the integer 2. In the end apply the catch exception. 
code of the program: import  java.io.*; class  AllEvenNum{    public static void  main(String[] args) {      try {         BufferedReader br1 =  new  BufferedReader ( new  InputStreamReader(  System.in));         System.out.println(&quot;Enter number : &quot;);          int  num = Integer.parseInt(br1.readLine());         System.out.println(&quot;Even Numbers:&quot;);          for  ( int  i=1;i <=num ; i++){         if (i%2==0 ){            System.out.print(i+&quot;,&quot;);       }          }     }      catch (Exception e){   e.printStackTrace();   }  } }
How to compile & run  Compiling: javac AllEvenNum.java Run: java AllEvenNum Note: This program cannot be run in jdeveloper
calculate area and perimeter of a circle This is a program to  calculate the area and perimeter of a circle
First of all name a class as &quot;CircleArea&quot; under  java  I/O package and define and integer r=1o, which is the radius of the circle. Now use  try  exception to handle errors and other exceptional events.  Now create the Math class in which all the mathematical functions are defined. This Math class can be imported from the java.lang.* package.  Formula for calculate area   area = java.lang.Math.PI*r*r; Formula for calculate area   perimeter =2*java.lang.Math.PI*r ; Before ending the program use the  Catch  mechanism that detects and catch user input errors.
code of the program: import  java.io.*; class  CircleArea{ public static void main(String[] args){ int r=10; try { double area = java.lang.Math.PI*r*r;   System.out.println(&quot;Area of Circle : &quot;+area); double  perimeter =2*java.lang.Math.PI*r ; System.out.println(&quot;Perimeter of Circle : &quot;+perimeter); } catch(Exception e){ System.out.println(&quot;Error : &quot;+e); }  } }
How to compile & run  Compiling: javac CircleArea.java Run: java CircleArea Out put: Area of Circle : 314.1592653589793 Perimeter of Circle : 62.83185307179586
Calculate Factorial Of A Given Number Here is a program to calculate factorial of a  given number. First of all define a class &quot;Factorial&quot; under the Java I/O package.  Define 'a' as an integer with value 10. Take an integer variable as fact=1.   Now applying for loop with conditions as integer i=1(intializer),  i<=a and i++ as increment operator. So output result will be like fact=fact*i.  Print the result.
code of the program: import java.io.*;  class  Factorial { public static void main(String[] args) { try{ int a= 10; int fact= 1; System.out.println(&quot;Factorial of &quot; +a+ &quot;:&quot;); for (int i= 1; i<=a; i++){   fact=fact*i;   } System.out.println(fact); } catch (Exception e){   e.printStackTrace();   } } }
How to compile & run  Compiling: javac Factorial.java Run: java Factorial Out put:   Factorial of 10: 3628800
calculating area and perimeter of a rectangle Here is a program for calculating the area and  perimeter of a rectangle. 
First of all create a class named  RecArea  under Java.io package. Now define two integer variable ' length'  and ' width‘  with values 10 and 50.  Calculate area and Perimeter by applying the formulas area = length*width; perimiter = 2*(length+width); Print result in the console
import java.io.*;  class  RecArea { public static void main(String[] args)  { int length=10; int width=50; try{  int area = length*width; System.out.println(&quot;Area of Rectangle : &quot;+area); int perimiter = 2*(length+width); System.out.println(&quot;Perimeter: &quot; + perimiter); } catch(Exception e){System.out.println(&quot;Error : &quot;+e);} } }
How to compile & run  Compiling: javac  RecArea .java Run: java  RecArea Out put:   Area of Rectangle : 500   Perimeter: 120
program to construct a triangle with the ‘*’ Here is the program for constructing a shape of  triangle by using '*'.
Make a class named ‘Triangle‘. Define an integer ' a ‘ with value 5. Now apply the  for  loop and define an integer  'i'  and it should be either less than or equal to the integer &quot;a“. for (int i=1; i<a;i++ ) Again define another integer type variable &quot; j &quot; in another for loop. for (int j=1; j<=i;j++ ) Here in the second for loop &quot;j&quot; the number of times we have to print *(You can take any other object instead of *) . 
import java.io.*;  Class Triangle{  public static void main(String[] args) { try{  int a= 5; for (int i=1; i<a;i++ ){ for (int j=1; j<=i;j++ ){   System.out.print(&quot;*&quot;); } System.out.println(&quot;&quot;); } } catch(Exception e){} } }
How to compile & run  Compiling: javac Triangle.java Run: java Triangle Out put:   *   **   ***   ****
Listing out leap years between certain period Here is the program for finding and listing out  the leap years between two years. In the  following example we have to find out the leap  years between 1990 and 2010.
First define the two years under a class &quot;leapyears&quot;. Let i = 2010 and n=1990. Now with the help of for loop method initialize the year as n=1990 and n<=i. Also apply the increment statement in the loop as we have to check one by one.   As we know a leap year is divisible by 4, define an integer l=n%4.  So if 'n' is divisible by 4 or l=0, then the particular year can be a leap year. For checking this, apply the  if  statement and if this satisfies then, the year will be a leap year. For listing out each year write &quot;+n&quot; in the System.out.println. 
import java.io.*;  Class Leapyears {  public static void main(String[] args) {  { int i=2010; int n; for (n=1990; n<=i ; n++) { int l=n%4; if (l==0){   System.out.println(&quot;leap year: &quot;+n);   } } } } }
How to compile & run  Compiling: javac Leapyears.java Run: java Leapyears Out put:   leap year: 1992   leap year: 1996 leap year: 2000 leap year: 2004 leap year: 2008

More Related Content

What's hot (20)

PDF
Java conditional statements
Kuppusamy P
 
PPTX
Operators in java
Then Murugeshwari
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPTX
Arrays in Java
Abhilash Nair
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Presentation on Core java
mahir jain
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPTX
JAVA ENVIRONMENT
josemachoco
 
PDF
Generics
Ravi_Kant_Sahu
 
PPTX
Constructor ppt
Vinod Kumar
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PDF
The New JavaScript: ES6
Rob Eisenberg
 
PPT
Collection Framework in java
CPD INDIA
 
PPTX
OOPS In JAVA.pptx
Sachin33417
 
PPTX
constructors in java ppt
kunal kishore
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
for loop in java
Majid Ali
 
PPTX
Java string handling
Salman Khan
 
Java conditional statements
Kuppusamy P
 
Operators in java
Then Murugeshwari
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Arrays in Java
Abhilash Nair
 
Control Statements in Java
Niloy Saha
 
Presentation on Core java
mahir jain
 
Methods in Java
Jussi Pohjolainen
 
JAVA ENVIRONMENT
josemachoco
 
Generics
Ravi_Kant_Sahu
 
Constructor ppt
Vinod Kumar
 
Applets in java
Wani Zahoor
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
The New JavaScript: ES6
Rob Eisenberg
 
Collection Framework in java
CPD INDIA
 
OOPS In JAVA.pptx
Sachin33417
 
constructors in java ppt
kunal kishore
 
Type casting in java
Farooq Baloch
 
for loop in java
Majid Ali
 
Java string handling
Salman Khan
 

Viewers also liked (18)

PPT
Ch01 basic-java-programs
James Brotsos
 
PDF
Java programming-examples
Mumbai Academisc
 
DOCX
Java codes
Hussain Sherwani
 
PPTX
Introduction to java
Veerabadra Badra
 
PPTX
Java simple programs
VEERA RAGAVAN
 
PDF
Java Programming Basics
Rkrishna Mishra
 
PPTX
Java Reflection Concept and Working
Software Productivity Strategists, Inc
 
PDF
Java Programming
Anjan Mahanta
 
PPT
Lecture3
Muhammad Zubair
 
ODP
Preparing Java 7 Certifications
Giacomo Veneri
 
PPTX
Java: Collections
Arthur Emanuel
 
DOC
Advance java practicalty bscit sem5
ashish singh
 
PPT
java collections
javeed_mhd
 
PPTX
Java collections
Amar Kutwal
 
PDF
Java Collections Tutorials
Prof. Erwin Globio
 
PPTX
Introduction to Java Programming
One97 Communications Limited
 
PDF
Java Certification by HUJAK - 2015-05-12 - at JavaCro'15 conference
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PPT
Graphics programming in Java
Tushar B Kute
 
Ch01 basic-java-programs
James Brotsos
 
Java programming-examples
Mumbai Academisc
 
Java codes
Hussain Sherwani
 
Introduction to java
Veerabadra Badra
 
Java simple programs
VEERA RAGAVAN
 
Java Programming Basics
Rkrishna Mishra
 
Java Reflection Concept and Working
Software Productivity Strategists, Inc
 
Java Programming
Anjan Mahanta
 
Lecture3
Muhammad Zubair
 
Preparing Java 7 Certifications
Giacomo Veneri
 
Java: Collections
Arthur Emanuel
 
Advance java practicalty bscit sem5
ashish singh
 
java collections
javeed_mhd
 
Java collections
Amar Kutwal
 
Java Collections Tutorials
Prof. Erwin Globio
 
Introduction to Java Programming
One97 Communications Limited
 
Java Certification by HUJAK - 2015-05-12 - at JavaCro'15 conference
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Graphics programming in Java
Tushar B Kute
 
Ad

Similar to Simple Java Programs (20)

PPT
Algorithms with-java-1.0
BG Java EE Course
 
PDF
Microsoft word java
Ravi Purohit
 
PDF
The sample program for above series in JAVA will be like belowimpo.pdf
rajat630669
 
PPTX
Lab01.pptx
KimVeeL
 
PPT
Ch5(loops)
Uğurcan Uzer
 
PDF
Oot practical
Vipin Rawat @ daya
 
DOCX
Programs of java
shafiq sangi
 
PPTX
Java Chapter 05 - Conditions & Loops: part 5
DanWooster1
 
PPT
How to Program
Damian T. Gordon
 
PPTX
Lab101.pptx
KimVeeL
 
DOCX
Cis 355 ilab 2 of 6
ashhadiqbal
 
PDF
merged_document_3
tori hoff
 
PDF
Java practical(baca sem v)
mehul patel
 
DOCX
Java programs
Dr.M.Karthika parthasarathy
 
PPTX
Core java
Uday Sharma
 
PPT
Java căn bản - Chapter6
Vince Vo
 
DOCX
Java Practice Set
Gaurav Dixit
 
PDF
Java_Programming_by_Example_6th_Edition.pdf
JayveeCultivo
 
PPTX
Lecture no 3
hasi071
 
PDF
Sam wd programs
Soumya Behera
 
Algorithms with-java-1.0
BG Java EE Course
 
Microsoft word java
Ravi Purohit
 
The sample program for above series in JAVA will be like belowimpo.pdf
rajat630669
 
Lab01.pptx
KimVeeL
 
Ch5(loops)
Uğurcan Uzer
 
Oot practical
Vipin Rawat @ daya
 
Programs of java
shafiq sangi
 
Java Chapter 05 - Conditions & Loops: part 5
DanWooster1
 
How to Program
Damian T. Gordon
 
Lab101.pptx
KimVeeL
 
Cis 355 ilab 2 of 6
ashhadiqbal
 
merged_document_3
tori hoff
 
Java practical(baca sem v)
mehul patel
 
Core java
Uday Sharma
 
Java căn bản - Chapter6
Vince Vo
 
Java Practice Set
Gaurav Dixit
 
Java_Programming_by_Example_6th_Edition.pdf
JayveeCultivo
 
Lecture no 3
hasi071
 
Sam wd programs
Soumya Behera
 
Ad

Recently uploaded (20)

PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 

Simple Java Programs

  • 2. Comparing Two Numbers This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one .
  • 3. First of all, name a class &quot;Comparing&quot; and take two variables in this class (ie, a & b). Here we have taken a=5 and b=10, Now we have to find out whether a=b, a>b or b>a. To find out this apply if and else condition one by one. Now apply the condition &quot;if (a=b)&quot;, if this satisfies then print both are equal. If this doesn't satisfy, then check whether a>b by applying the &quot;else if&quot; condition and print the message &quot;a is greater than b”. Again this doesn't satisfy then 'else' condition as shown in the example will show that b is greater than a. 
  • 4. code of the program: class   Comparing{    public static void  main(String[] args) {      int  a=5, b=10;      if  (a == b){        System.out.println(&quot;Both are equal&quot;);     }      else if (a>b){       System.out.println(&quot;a is greater than b&quot;);      }      else {        System.out.println(&quot;b is greater than a&quot;);      }   } }
  • 5. How to compile & run Compiling: javac Comparing.java Run: java Comparing Out put: b is greater than a
  • 6. Program to list all even numbers between two numbers This is a program for listing out all the even numbers between two numbers.
  • 7. For this first create a class named AllEvenNum under the java.io package. Now use the try/catch exception to avoid any kind of input error. After this create a buffer class in which all the input data are stored and modified. Then give message as to &quot;Enter number&quot; in the System method.  As we have to find out all the even numbers between 1 and the input number, define an integer variable 'num'. Now apply ParseInt method that parses the string character into decimal integer. Apply for loop in which define an integer i=1 and i<=  num also with an increment operator. Then apply the if condition that i/2=0 i.e. to find even numbers which are divided by the integer 2. In the end apply the catch exception. 
  • 8. code of the program: import  java.io.*; class  AllEvenNum{    public static void  main(String[] args) {      try {        BufferedReader br1 =  new  BufferedReader ( new  InputStreamReader( System.in));        System.out.println(&quot;Enter number : &quot;);         int  num = Integer.parseInt(br1.readLine());        System.out.println(&quot;Even Numbers:&quot;);         for  ( int  i=1;i <=num ; i++){        if (i%2==0 ){            System.out.print(i+&quot;,&quot;);     }        }     }      catch (Exception e){ e.printStackTrace(); }  } }
  • 9. How to compile & run Compiling: javac AllEvenNum.java Run: java AllEvenNum Note: This program cannot be run in jdeveloper
  • 10. calculate area and perimeter of a circle This is a program to calculate the area and perimeter of a circle
  • 11. First of all name a class as &quot;CircleArea&quot; under java I/O package and define and integer r=1o, which is the radius of the circle. Now use try exception to handle errors and other exceptional events. Now create the Math class in which all the mathematical functions are defined. This Math class can be imported from the java.lang.* package. Formula for calculate area area = java.lang.Math.PI*r*r; Formula for calculate area perimeter =2*java.lang.Math.PI*r ; Before ending the program use the Catch mechanism that detects and catch user input errors.
  • 12. code of the program: import  java.io.*; class  CircleArea{ public static void main(String[] args){ int r=10; try { double area = java.lang.Math.PI*r*r; System.out.println(&quot;Area of Circle : &quot;+area); double perimeter =2*java.lang.Math.PI*r ; System.out.println(&quot;Perimeter of Circle : &quot;+perimeter); } catch(Exception e){ System.out.println(&quot;Error : &quot;+e); } } }
  • 13. How to compile & run Compiling: javac CircleArea.java Run: java CircleArea Out put: Area of Circle : 314.1592653589793 Perimeter of Circle : 62.83185307179586
  • 14. Calculate Factorial Of A Given Number Here is a program to calculate factorial of a given number. First of all define a class &quot;Factorial&quot; under the Java I/O package. Define 'a' as an integer with value 10. Take an integer variable as fact=1.  Now applying for loop with conditions as integer i=1(intializer),  i<=a and i++ as increment operator. So output result will be like fact=fact*i. Print the result.
  • 15. code of the program: import java.io.*; class Factorial { public static void main(String[] args) { try{ int a= 10; int fact= 1; System.out.println(&quot;Factorial of &quot; +a+ &quot;:&quot;); for (int i= 1; i<=a; i++){ fact=fact*i; } System.out.println(fact); } catch (Exception e){ e.printStackTrace(); } } }
  • 16. How to compile & run Compiling: javac Factorial.java Run: java Factorial Out put: Factorial of 10: 3628800
  • 17. calculating area and perimeter of a rectangle Here is a program for calculating the area and perimeter of a rectangle. 
  • 18. First of all create a class named RecArea under Java.io package. Now define two integer variable ' length' and ' width‘ with values 10 and 50. Calculate area and Perimeter by applying the formulas area = length*width; perimiter = 2*(length+width); Print result in the console
  • 19. import java.io.*; class RecArea { public static void main(String[] args) { int length=10; int width=50; try{ int area = length*width; System.out.println(&quot;Area of Rectangle : &quot;+area); int perimiter = 2*(length+width); System.out.println(&quot;Perimeter: &quot; + perimiter); } catch(Exception e){System.out.println(&quot;Error : &quot;+e);} } }
  • 20. How to compile & run Compiling: javac RecArea .java Run: java RecArea Out put: Area of Rectangle : 500 Perimeter: 120
  • 21. program to construct a triangle with the ‘*’ Here is the program for constructing a shape of triangle by using '*'.
  • 22. Make a class named ‘Triangle‘. Define an integer ' a ‘ with value 5. Now apply the for loop and define an integer 'i' and it should be either less than or equal to the integer &quot;a“. for (int i=1; i<a;i++ ) Again define another integer type variable &quot; j &quot; in another for loop. for (int j=1; j<=i;j++ ) Here in the second for loop &quot;j&quot; the number of times we have to print *(You can take any other object instead of *) . 
  • 23. import java.io.*; Class Triangle{ public static void main(String[] args) { try{ int a= 5; for (int i=1; i<a;i++ ){ for (int j=1; j<=i;j++ ){ System.out.print(&quot;*&quot;); } System.out.println(&quot;&quot;); } } catch(Exception e){} } }
  • 24. How to compile & run Compiling: javac Triangle.java Run: java Triangle Out put: * ** *** ****
  • 25. Listing out leap years between certain period Here is the program for finding and listing out the leap years between two years. In the following example we have to find out the leap years between 1990 and 2010.
  • 26. First define the two years under a class &quot;leapyears&quot;. Let i = 2010 and n=1990. Now with the help of for loop method initialize the year as n=1990 and n<=i. Also apply the increment statement in the loop as we have to check one by one.  As we know a leap year is divisible by 4, define an integer l=n%4. So if 'n' is divisible by 4 or l=0, then the particular year can be a leap year. For checking this, apply the if statement and if this satisfies then, the year will be a leap year. For listing out each year write &quot;+n&quot; in the System.out.println. 
  • 27. import java.io.*; Class Leapyears { public static void main(String[] args) { { int i=2010; int n; for (n=1990; n<=i ; n++) { int l=n%4; if (l==0){ System.out.println(&quot;leap year: &quot;+n); } } } } }
  • 28. How to compile & run Compiling: javac Leapyears.java Run: java Leapyears Out put: leap year: 1992 leap year: 1996 leap year: 2000 leap year: 2004 leap year: 2008