Your SlideShare is downloading. ×
0
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Simple Java Programs
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Simple Java Programs

56,446

Published on

Published in: Education
2 Comments
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
56,446
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
632
Comments
2
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. <ul><li>SAMPLE JAVA PROGRAMS </li></ul>
  • 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. <ul><li>First of all, name a class &amp;quot;Comparing&amp;quot; and take two variables in this class (ie, a &amp; b). Here we have taken a=5 and b=10, </li></ul><ul><li>Now we have to find out whether a=b, a&gt;b or b&gt;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 &amp;quot;if (a=b)&amp;quot;, if this satisfies then print both are equal. </li></ul><ul><li>If this doesn&apos;t satisfy, then check whether a&gt;b by applying the &amp;quot;else if&amp;quot; condition and print the message &amp;quot;a is greater than b”. </li></ul><ul><li>Again this doesn&apos;t satisfy then &apos;else&apos; condition as shown in the example will show that b is greater than a.  </li></ul>
  • 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(&amp;quot;Both are equal&amp;quot;);     }      else if (a&gt;b){       System.out.println(&amp;quot;a is greater than b&amp;quot;);      }      else {        System.out.println(&amp;quot;b is greater than a&amp;quot;);      }   } } </li></ul>
  • 5. How to compile &amp; run <ul><li>Compiling: </li></ul><ul><li>javac Comparing.java </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. 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. <ul><li>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. </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 &amp;quot;Enter number&amp;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 &apos;num&apos;. </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&lt;=  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. <ul><li>code of the program: </li></ul><ul><li>import  java.io.*; </li></ul><ul><li>class  AllEvenNum{    public static void  main(String[] args) {      try {        BufferedReader br1 =  new  BufferedReader </li></ul><ul><li>( new  InputStreamReader( System.in));        System.out.println(&amp;quot;Enter number : &amp;quot;);         int  num = Integer.parseInt(br1.readLine());        System.out.println(&amp;quot;Even Numbers:&amp;quot;);         for  ( int  i=1;i &lt;=num ; i++){        if (i%2==0 ){            System.out.print(i+&amp;quot;,&amp;quot;);     }        }     }      catch (Exception e){ </li></ul><ul><li> e.printStackTrace(); </li></ul><ul><li> }  } </li></ul><ul><li>} </li></ul>
  • 9. How to compile &amp; run <ul><li>Compiling: </li></ul><ul><li>javac AllEvenNum.java </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. 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. <ul><li>First of all name a class as &amp;quot;CircleArea&amp;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. <ul><li>code of the program: </li></ul><ul><li>import  java.io.*; </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(&amp;quot;Area of Circle : &amp;quot;+area); </li></ul><ul><li>double perimeter =2*java.lang.Math.PI*r ; </li></ul><ul><li>System.out.println(&amp;quot;Perimeter of Circle : &amp;quot;+perimeter); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e){ </li></ul><ul><li>System.out.println(&amp;quot;Error : &amp;quot;+e); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 13. How to compile &amp; run <ul><li>Compiling: </li></ul><ul><li>javac CircleArea.java </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. 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 &amp;quot;Factorial&amp;quot; under the Java I/O package. </li></ul><ul><li>Define &apos;a&apos; 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&lt;=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. <ul><li>code of the program: </li></ul><ul><li>import java.io.*; </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(&amp;quot;Factorial of &amp;quot; +a+ &amp;quot;:&amp;quot;); </li></ul><ul><li>for (int i= 1; i&lt;=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. How to compile &amp; run <ul><li>Compiling: </li></ul><ul><li>javac Factorial.java </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. 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. <ul><li>First of all create a class named RecArea under Java.io package. Now define two integer variable &apos; length&apos; and &apos; 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. <ul><li>import java.io.*; </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(&amp;quot;Area of Rectangle : &amp;quot;+area); </li></ul><ul><li>int perimiter = 2*(length+width); </li></ul><ul><li>System.out.println(&amp;quot;Perimeter: &amp;quot; + perimiter); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e){System.out.println(&amp;quot;Error : &amp;quot;+e);} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 20. How to compile &amp; 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. 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 &apos;*&apos;. </li></ul>
  • 22. <ul><li>Make a class named ‘Triangle‘. </li></ul><ul><li>Define an integer &apos; a ‘ with value 5. </li></ul><ul><li>Now apply the for loop and define an integer &apos;i&apos; and it should be either less than or equal to the integer &amp;quot;a“. </li></ul><ul><li>for (int i=1; i&lt;a;i++ ) </li></ul><ul><li>Again define another integer type variable &amp;quot; j &amp;quot; in another for loop. </li></ul><ul><li>for (int j=1; j&lt;=i;j++ ) </li></ul><ul><li>Here in the second for loop &amp;quot;j&amp;quot; the number of times we have to print *(You can take any other object instead of *) .  </li></ul>
  • 23. <ul><li>import java.io.*; </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&lt;a;i++ ){ </li></ul><ul><li>for (int j=1; j&lt;=i;j++ ){ </li></ul><ul><li> System.out.print(&amp;quot;*&amp;quot;); </li></ul><ul><li>} </li></ul><ul><li>System.out.println(&amp;quot;&amp;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. How to compile &amp; run <ul><li>Compiling: </li></ul><ul><li>javac Triangle.java </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. 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. <ul><li>First define the two years under a class &amp;quot;leapyears&amp;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&lt;=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 &apos;n&apos; 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 &amp;quot;+n&amp;quot; in the System.out.println.  </li></ul>
  • 27. <ul><li>import java.io.*; </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&lt;=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(&amp;quot;leap year: &amp;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. How to compile &amp; run <ul><li>Compiling: </li></ul><ul><li>javac Leapyears.java </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>

×