Professional Documents
Culture Documents
Simple Java Programs
Simple Java Programs
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"); } } }
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 "Enter number" 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("Enter number : "); int num = Integer.parseInt(br1.readLine()); System.out.println("Even Numbers:"); for (int i=1;i <=num ; i++){ if(i%2==0 ){ System.out.print(i+","); } } } catch(Exception e){ e.printStackTrace(); } } }
First of all name a class as "CircleArea" 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.
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("Area of Rectangle : "+area); int perimiter = 2*(length+width); System.out.println("Perimeter: " + perimiter); } catch(Exception e){System.out.println("Error : "+e);} } }
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 "a. for (int i=1; i<a;i++ ) Again define another integer type variable "j" in another for loop. for (int j=1; j<=i;j++ ) Here in the second for loop "j" 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("*"); } System.out.println(""); } } catch(Exception e){} } }
First define the two years under a class "leapyears". 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 "+n" 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("leap year: "+n); } } } } }