Category Archives: core java

List Even Numbers Java Example

Category : core java , Uncategorized

 

Program to List Even Numbers in Java.

/**
 * Program to List Even Numbers Java Example.
 * @author javawithease
 */
public class ListEvenNumberExample {
	static void listEvenNumber(int limitNumber){
		//Print limit number.
		System.out.println("Even numbers between 1 and " + limitNumber + ":");
        
		//Find even numbers.
                for(int i=1; i <= limitNumber; i++){               
                   //Test even number condition.
                   if( i % 2 == 0){
                      System.out.println(i);
                   }
              }
	}	
	
	public static void main(String args[]){ 
		//method call 
                listEvenNumber(60); 
	}
}

Output:

Even numbers between 1 and 60: 
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60

Download this example.
 


Leap year java program

Category : core java , Uncategorized

Leap year conditions:

if (year is not exactly divisible by 4) then (it is a common year)
else if (year is not exactly divisible by 100) then (it is a leap year)
else if (year is not exactly divisible by 400) then (it is a common year)
else (it is a leap year).

Program to Determine If Year Is Leap Year Java.

/**
 * Program to Determine If Year Is Leap Year Java Example.
 * @author javawithease
 */
public class LeapYearExample {	
 static void leapArea(int year){
  //Check Leap year condition.
  if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
	 System.out.println(year + ": Leap year.");
  }else{
	 System.out.println(year + ": Not a leap year.");
  }
 }	
	
 public static void main(String args[]){ 
  //method call 
  leapArea(2016); 
 }
}

Output:

2016: Leap year.

Download this example.
 


calculate-rectangle-perimeter-using-java-example-output

Category : core java , Uncategorized

 
Perimeter of a rectangle = 2 * (length + width).

Program to Calculate Rectangle Perimeter using Java.

/**
 * Program to Calculate Rectangle Perimeter using Java Example.
 * @author javawithease
 */
public class RectanglePerimeterExample {
	static void rectanglePerimeter(int length, int width) {
		  int perimeter = 2 * (length + width);
                  //Print Rectangle Area.
                  System.out.println("Rectangle Perimeter: " + perimeter);
	}	
	
	public static void main(String args[]){ 
		//method call 
		rectanglePerimeter(20, 30); 
	}
}

Output:

Rectangle Perimeter: 100

Download this example.
 


Calculate Rectangle Area using Java Example

Category : core java , Uncategorized

 
Area of a rectangle = length * width.

Program to Calculate Rectangle Area using Java.

/**
 * Program to Calculate Rectangle Area using Java Example.
 * @author javawithease
 */
public class RectangleAreaExample {
	static void rectangleArea(int length, int width){
                 //Calculate Rectangle Area.
		 int area = length * width;
                 //Print Rectangle Area.
                 System.out.println("Rectangle area: " + area);
	}	
	
	public static void main(String args[]){ 
		//method call 
		rectangleArea(20, 30); 
	}
}

Output:

Rectangle area: 600

Download this example.
 


Calculate Circle Perimeter using Java Example

Category : core java , Uncategorized

 
Perimeter of a circle = 2 * pi * r.
where:
r is the radius of the circle.
pi is a mathematical constant.

Program to Calculate Circle Perimeter using Java.

/**
 * Program to Calculate Circle Perimeter using Java Example.
 * @author javawithease
 */
public class CirlePerimeterExample {
	static void circlePerimeter(int radius){
		 //Calculate Circle Perimeter.
		 double perimeter = 2 * Math.PI * radius;
                 //Print Circle Area.
                 System.out.println("Circle perimeter: " + perimeter);
	}	
	
	public static void main(String args[]){ 
		//method call 
		circlePerimeter(20); 
	}
}

Output:

Circle perimeter: 125.66370614359172

Download this example.
 


Calculate Circle Area using Java Example

Category : core java , Uncategorized

 
Area of a circle = pi * r * r.
where:
r is the radius of the circle.
pi is a mathematical constant.

Program to Calculate Circle Area using Java.

/**
 * Program to Calculate Circle Area using Java Example.
 * @author javawithease
 */
public class CirleAreaExample {
	static void circleArea(int radius){
		 double area = Math.PI * radius * radius;
         //Print Circle Area.
         System.out.println("Circle area: " + area);
	}	
	
	public static void main(String args[]){ 
		//method call 
		circleArea(20); 
	}
}

Output:

Circle area: 1256.6370614359173

Download this example.
 


Reverse Number in Java

Category : core java , Uncategorized

 

Program to Reverse the digits of a Number.

/**
 * This program is used to reverse the digits of a number.
 * @author javawithease
 */
public class ReverseNumberExample {
	static void reverseNum(int num){
		//Print reverse number.
		System.out.println("Numer before reverse process: " + num);
		int newNum = 0, reminder, temp; 
		temp = num; 
		//Reverse the digit's of the number. 
		while(temp != 0){ 
			reminder = temp % 10; 
			newNum = newNum*10 + reminder; 
			temp = temp/10; 
		} 
		//Print reverse number.
		System.out.println("Numer after reverse process: " + newNum);
	}
	
	
	public static void main(String args[]){ 
		//method call 
		reverseNum(34267); 
	}
}

Output:

Numer before reverse process: 34267
Numer after reverse process: 76243

Download this example.
 


Swap Numbers in Java

Category : core java , Uncategorized

 

Program to swap two numbers using third or temp variable.

/**
 * This program is used to swap to numbers using temp variable.
 * @author javawithease
 */
public class SwapNumberExample {
  static void swapNumbers(int num1, int num2){ 
	int temp = num1;
        num1 = num2;
        num2 = temp;
	System.out.println("After swapping: "+ num1 + " and " + num2); 
  } 
	
  public static void main(String args[]){ 
	int num1 = 20; 
	int num2 = 30; 
	System.out.println("Before swapping:"+ num1 + " and " + num2); 
	//method call 
	swapNumbers(num1, num2); 
  }
}

Output:

Before swapping:20 and 30
After swapping: 30 and 20

Download this example.
 


Swap Two Numbers Without Using Third Variable in Java

Category : core java , Uncategorized

 

Program to swap two numbers without using third or temp variable.

/**
 * This program is used to swap two numbers without using third variable.
 * @author javawithease
 */
public class SwapNumbers {
      /**
       * This method is used to swap no.s without using third variable.
       * @param num1
       * @param num2
       */
      static void swapNumbers(int num1, int num2){
            num1 = num1 + num2;
            num2 = num1 - num2;
            num1 = num1 - num2;

            System.out.println("After swapping: "+ num1 + " and " + num2);
      }     

      public static void main(String args[]){
            int num1 = 20;
            int num2 = 30;
           System.out.println("Before swapping:"+ num1 + " and " + num2);
            //method call
            swapNumbers(num1, num2);
      }

}

Output:

Before swapping no.s are : 20 and 30
After swapping no.s are : 30 and 20

Download this example.
 


Prime Number Program in Java

Category : core java , Uncategorized

 

Program to find that given number is prime or not.

/**
 * This program is used to find that given number is prime or not.
 * @author javawithease
 */

public class PrimeNumber {
      /**
       * This method is used to find that given number is prime or not.
       * @param num
       */
      static void primeNumber(int num){
            int count = 0;
            //0 and 1 are not prime numbers.
            if(num == 0 || num == 1){
                  System.out.println(num + " is not prime.");
            }else{
                  for(int i = 1; i <= num/2; i++){
                        if(num % i == 0){
                              count++;
                        }
                  }
                  if(count > 1){
                        System.out.println(num + " is not prime.");
                  }else{
                        System.out.println(num + " is prime.");
                  }
            }
      }    

      public static void main(String args[]){
            //method call
            primeNumber(37);
      }
}

Output:

37 is prime.

Download this example.