Simple Java Programs


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Simple Java Programs

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