Java Program to check a String is palindrome or not by ignoring its case


  • A palindrome is a word that reads the same backward or forward.
  • Lets see what if the characters in a palindrome string are in different case. i.e should not be case sensitive. 
  • So now we will see how to check a string is palindrome or not by ignoring its case.
  • Write a function which checks  that string is palindrome or not by converting the original string to lowercase or uppercase.
  • Method should return true if it is palindrome, return false if not a palindrome.


#1 : Java Program to check given string is palindrome or not by ignoring its case.

  1. package com.instanceofjava;

  2. public class CheckPalindrome {
  3. public static boolean isPalindrome(String str) {
  4. StringBuffer strone=new StringBuffer(str.toLowerCase());
  5. StringBuffer strtwo=new StringBuffer(strone);
  6.  
  7.   strone.reverse();
  8.  
  9.   System.out.println("Orginal String ="+strtwo);
  10.   System.out.println("After Reverse ="+strone);
  11.  
  12. if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
  13. return true;
  14.     else
  15.     return false;
  16. }
  17. public static void main(String[] args) {
  18. boolean ispalindrome= isPalindrome("DeleveleD");
  19. System.out.println(ispalindrome);
  20.  
  21.     }

  22. }

Output

  1. Orginal String =deleveled
  2. After Reverse =deleveled
  3. true

check palindrome or not ignore case sensitive

Remove specified element from Java LinkedHashSet example

1.Basic java example program to remove particular element in linkedhashset
  • boolean remove(Object o)    This method used to remove specified element from Linkedhashset.

  1. package com.removeelementLinkedhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.       
  19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
  20.  
  21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
  22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
  23.   

  24.  
  25. System.out.println("LinkedHashSet after removal : ");
  26.  
  27. Iterator it=linkedhashset.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  
  34. }
  35.  
  36. }
     



Output:

  1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
  2. and example program, Concept and interview questions, Java Quiz]
  3. Was Java Quiz removed from LinkedHashSet ? true
  4. Java Interview Questions
  5. Java interview program
  6. Concept and example program
  7. Concept and interview questions


Basic Java Example Program to check even or add

1.Basic Java program to check even or add

  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class EvenOrOdd{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. Scanner in= new Scanner(System.in);
  8. System.out.println("Please enter number to check even or odd");
  9.  
  10.     int n=in.nextInt();
  11.    
  12.          
  13. if(n%2==0){
  14.   System.out.println(+n" is even number");
  15. } else{
  16.  
  17. System.out.println(+n" is even number");
  18.  
  19. }
  20. }
  21. }

Output:


  1. Please enter number to check even or odd
  2. 4
  3. 4 is even number




2.Basic Java program to check even or add from 1 to 10

  1. package com.BasicJavaProgramsExamples;
  2. public Class EvenOrOdd{ 
  3.  
  4. public static void main(String args[]) {
  5.  
  6. for (int a=1; a<=10; a=a++){
  7.  if (a%2==0)
  8.     System.out.println(a+" is even");
  9. else
  10.     System.out.println(a+" is odd");
  11.  
  12. }   
  13. }
  14. }

Output:

  1. 1 is odd
  2. 2 is even
  3. 3 is odd
  4. 4 is even
  5. 5 is odd
  6. 6 is even
  7. 7 is odd
  8. 8 is even
  9. 9 is odd
  10. 10 is even

3.Basic Java program to check even or add from an array

  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class EvenOrOdd{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
  8.  
  9. for (int a=0; a<=numbers .length; a=a++){
  10.  
  11.  if (numbers[i]%2==0)
  12.     System.out.println(numbers[i]+" is even number");
  13. else
  14.     System.out.println(numbers[i]+" is odd number");
  15.  
  16. }    

  17. }
  18. }

Output:


  1. 1 is odd number
  2. 2 is even number
  3. 3 is odd number
  4. 4 is even number
  5. 5 is odd number
  6. 6 is even number
  7. 7 is odd number
  8. 8 is even number
  9. 9 is odd number
  10. 10 is even number
 

Java Example program to calculate area of reactangle

In this tutorial we will see how to calculate Area of Rectangle.

1.Write a Basic java example  program to find area of Rectangle

  •  To calculate the area of rectangle we need to ask user to enter length and width of the rectangle so that we can calculate area of rectangle using formula area of rectangle=length*width



  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class AreaOfRectangle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. Scanner in= new Scanner(System.in);
  8. System.out.println("Please enter length of a rectangle");
  9.  
  10.     double length=in.nextDouble();
  11.      
  12. System.out.println("Please enter width of a rectangle");
  13.  
  14.     double width=in.nextDouble();
  15.  
  16. /*
  17.  *Area of a rectangle is
  18.  *Area= length*width;
  19.  *
  20.  */
  21.          
  22.     double area=length*width;
  23.       
  24.     System.out.println("Area of the Rectangle="+area);
  25.  
  26. }
  27. }

Output:

  1. Please enter length of a rectangle
  2. 4
  3. Please enter width of a rectangle
  4. 8
  5. Area of the circle =32.0




2.Write a Basic java example program to find Area of rectangle without user interaction
 

  1. package com.BasicJavaProgramsExamples;
  2. public Class AreaOfRectangle{ 
  3.  
  4. public static void main(String args[]) {
  5.  
  6.  
  7.   double length=4;
  8.      
  9.   double width=8;
  10.  
  11. /*
  12.  *Area of a rectangle is
  13.  *Area= length*width;
  14.  *
  15.  */
  16.          
  17.     double area=length*width;
  18.       
  19.     System.out.println("Area of the Rectangle="+area);
  20.  
  21. }
  22. }

Output:

  1. Area of the circle =32.0
1.Write a Basic java example  program to find perimeter of Rectangle

  •  To calculate the perimeter of rectangle we need to ask user to enter length and width of the rectangle so that we can calculate perimeter of rectangle using formula perimeter of rectangle=2*length*width



  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class PerimeterOfRectangle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. Scanner in= new Scanner(System.in);
  8. System.out.println("Please enter length of a rectangle");
  9.  
  10.     double length=in.nextDouble();
  11.      
  12. System.out.println("Please enter width of a rectangle");
  13.  
  14.     double width=in.nextDouble();
  15.  
  16. /*
  17.  *perimeter of a rectangle is
  18.  *perimeter= length*width;
  19.  *
  20.  */
  21.          
  22.     double perimeter=length*width;
  23.       
  24.     System.out.println("Perimeter of the Rectangle="+perimeter);
  25.  
  26. }
  27. }

Output:

  1. Please enter length of a rectangle
  2. 4
  3. Please enter width of a rectangle
  4. 8
  5. Perimeter of the circle =64.0

Basic Java program to find area and perimeter of circle

1.Write a Basic java example  program to find area of circle

  •  To calculate the area of circle we need to ask user to enter radius of the circle so that we can calculate area of circle using formula area of circle =PI* radius * radius



  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class AreaOfCirle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. int radius = 0;
  8. Scanner in= new Scanner(System.in);
  9. System.out.println("Please enter radius of a circle");
  10.  
  11.     radius=in.nextInt();
  12.      
  13. /*
  14.  * where r is a radius of a circle then Area of a circle is
  15.  *Area= pi * r * r
  16.  *
  17.  */
  18.          
  19.     double area=Math.PI* radius * radius;
  20.       
  21.     System.out.println("Area of the circle ="+area);
  22.  
  23. }
  24. }

Output:

  1. Please enter radius of a circle
  2. 23
  3. Area of the circle =1661.9025137490005




2.Write a Basic java example program to find perimeter of circle

  •  To calculate the perimeter  of circle we need to ask user to enter radius of the circle so that we can calculate perimeter  of circle using formula area of circle =2* PI* radius
  
  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class PerimeterOfCirle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. int radius = 0;
  8. Scanner in= new Scanner(System.in);
  9. System.out.println("Please enter radius of a circle");
  10.  
  11.     radius=in.nextInt();
  12.      
  13. /*
  14.  * where r is a radius of a circle then perimeter of a circle is
  15.  *Area= pi * r * r
  16.  *
  17.  */
  18.          
  19.     double perimeter =2* Math.PI* radius;
  20.  
  21.     System.out.println("Perimeter of the circle ="+perimeter );
  22.  
  23. }
  24. }

Output:

  1. Please enter radius of a circle
  2. 12
  3. Perimeter of the circle =75.39822368615503

Select Menu