Palindrome program in Java with Examples & Programs



Palindrome: Palindrome is a Word, number (or) sequence of characters are the same after the reverse of the word (or) number.
Example of Palindrome:
Number Palindrome Examples: 6556, 343, 1441, 24542,757.
String Palindrome Examples: level, madam, refer, rotor, lol.
Example of number Palindrome:
class Palindrome1
 public static void main(String args[])
 { 
 int i,sum=0,temp;   
 int n=757;
 temp=n;   
 while(n>0)
 {   
  i=n%10;   
  sum=(sum*10)+i;   
  n=n/10;   
  }   
  if(temp == sum)   
   System.out.println(sum + " is a palindrome number ");  
  else
   System.out.println(sum + "is not palindrome");  
}
}

Output :
757 is a palindrome number.



To check Palindrome or not for user entered number:
import java.util.Scanner;
class Palindrome2
 public static void main(String args[])
 { 
 int i,sum=0,temp;  
 Scanner s = new Scanner(System.in);
 System.out.print("Enter the value of N = ");
 int n = s.nextInt();
 temp = n;   
 while(n>0)
 {   
  i=n%10;   
  sum=(sum*10)+i;   
  n=n/10;   
  }   
  if(temp == sum)   
   System.out.println(sum + " is palindrome number ");  
  else
   System.out.println(sum + "is not palindrome");  
}
}

Example of String Palindrome:
import java.util.Scanner;
class Palindrome
{
 public static void main(String[] args)
 {
  String original,reverse = "";
  Scanner in = new Scanner(System.in);
  System.out.println("Enter the Text");
  original = in.nextLine();
  int length = original.length();
  for(int i = length - 1;i >= 0; i--)
  {
  reverse = reverse + original.charAt(i);
  }
  if(original.equals(reverse))
  System.out.println(reverse + " is a Palindrome");
  else
  System.out.println(reverse + " is Not a Palindrome");
  }
}

Output:

Enter the Text
madam
madam is a Palindrome
Share:

Prime Number Program in Java Using While loop & For Loop

Prime Number: A number that is divisible only by itself and 1 is called Prime number.
Examples: 2, 3, 5, 7, 11, 13, 17, 19 etc.
Prime number programs in Java:
Program to display the prime numbers from 1 to 100
public class Prime
{
 public static void main(String[] args)
 {
  int limit = 100;
  System.out.println("Prime numbers between 1 and 100");
      for(int i=2; i < limit; i++)
     {
      boolean isPrime = true;
       for(int j=2; j < i ; j++)
        {
         if(i % j == 0)
         {
           isPrime = false;
            break;
          }
         }
     if(isPrime)
    System.out.print(i + " ");
   }
 }
}

Output:
Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Program to display prime numbers from 1 to n:

 

import java.util.Scanner;

public class Prime2

{

 public static void main(String[] args)

 {

 Scanner s1 = new Scanner(System.in);

 System.out.println("Enter the value of N");

 int n = s1.nextInt();

    for(int i=2; i < n; i++)

     {

      boolean isPrime = true;

       for(int j=2; j < i ; j++)

        {

         if(i % j == 0)

         {

           isPrime = false;

            break;

          }

         }

     if(isPrime)

    System.out.print(i + " ");

   }

 }

}

Output:

Enter the value of N

40

2 3 5 7 11 13 17 19 23 29 31 37


Prime Number Program using Method in Java:

class Prime3

{

 void ToCheckPrime(int n)

 {

  int i,j=0,k=0;

  j = n/2;

  if(n==0||n==1)

  {

  System.out.println(n+" is not prime number");     

  }

 else

   { 

   for(i = 2;i <= j;i++)

   {     

    if(n%i == 0)

   {     

     System.out.println(n+" is not prime number");     

     k = 1;     

     break;     

    }     

   }     

   if(k == 0) 

   {

    System.out.println(n+" is prime number");

   } 

  }

 }

public static void main(String[] args)

 {

  Prime3 p1 = new Prime3();

  p1.ToCheckPrime(1);

  p1.ToCheckPrime(5);

  p1.ToCheckPrime(8);

 }

}

Output:

1 is not prime number

5 is prime number

8 is not prime number

Program to Check Prime Number using a while loop:

import java.util.Scanner;

class Prime4

{

public static void main(String[] args)

 {

 Scanner s1 = new Scanner(System.in);

 System.out.println("Enter the value of N");

 int n = s1.nextInt();

 int i = 2;

 boolean flag = false;

 while(i <= n/2)

 {

  if(n % i == 0)

  {

   flag = true;

   break;

  }

 ++i;

 }

 if (!flag)

 System.out.println(n + " is a prime number.");

 else

 System.out.println(n + " is not a prime number.");

 }

}


Output:

Enter the value of N

15

15 is not a prime number.


Find prime numbers between two numbers:

import java.util.Scanner; 

public class Prime5

 { 

   public static void main(String[] args)

 { 

       Scanner s = new Scanner(System.in); 

       System.out.print("Enter the first number = "); 

       int firstNumber = s.nextInt(); 

       System.out.print("Enter the second number = "); 

       int secondNumber = s.nextInt(); 

       System.out.println(" The prime numbers between " + firstNumber + " and " + secondNumber); 

       for (int i = firstNumber; i <= secondNumber; i++)

           { 

           if (isPrime(i))

           { 

               System.out.println(i); 

           } 

       } 

   } 

   public static boolean isPrime(int n) { 

       if (n <= 1)

       { 

           return false; 

       } 

       for (int i = 2; i <= Math.sqrt(n); i++)

        { 

           if (n % i == 0)

           { 

               return false; 

           } 

       } 

       return true; 

   } 

 

 Output:


Enter the first number = 2

Enter the second number = 15

The prime numbers between 2 and 15

2

3

5

7

11

13

Share:

Switch Statement in Java | Java Switch With Examples

Switch statement in Java: If several options are available then it isn’t recommended to take nested if-else we should go for switch statement so that readability of the code will be improved. “Switch” is a keyword, by using which we can create a selection statement with multiple choices. Multiple choices can be constructed by using a “Case” keyword.
Syntax:
Switch(expression)
{
 case exp1:
statements;
break;
case exp2:
statements;
break;
…………….
…………….
…………….
case expression:
statements;
break;
default:
statements;
}

Explanation: In the above syntax, switch, case, break are keywords. Expr1, expr2 is known as “case labels”. Statements inside case expression need not be closed in braces.  Break statement causes an exit from the switch statement.

 The following rules apply to a switch statement:
1) The expression used in a switch statement must have an integral or enumerated type.
2) You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
3) The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
 4) When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
5) When the break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
6) Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
7) A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Program:
import java.util.Scanner;
class SwitchExample
{
public static void main(String[] args)
{
 int number;
 Scanner in = new Scanner(System.in);
 System.out.println("Enter a number");
 number = in.nextInt();
 switch(number)
 {
  case 1:
  System.out.println("Welcome to TutorialWeb.org");
  break;
  case 2:
  System.out.println("I am Java programmer");
  break;
  case 3:
  System.out.println("Java programming in TutorialWeb.org");
  break;
  default:
  System.out.println("Switch Program Example");
  break;
 }
}
}


Output:
Enter a number
1
Welcome to TutorialWeb.org

Explanation:
1) The integer value “1” is assigned to number variable.
2) After switch case decides then 1st case block of code gets executed.
3) Switch case executes code from top to bottom. It will validate case number with the variable number.
4) If no match is found then it will jump to the next case. It checks until found if the case is not found, the default will be executed.
Program:
import java.util.Scanner;
class SwitchMonthExample
{
public static void main(String[] args)
{
 int number;
 Scanner in = new Scanner(System.in);
 System.out.println("Enter a number");
 number = in.nextInt();
 switch(number)
 {
  case 1:
  System.out.println("January");
  break;
  case 2:
  System.out.println("February");
  break;
  case 3:
  System.out.println("March");
  break;
  case 4:
  System.out.println("April");
  break;
  case 5:
  System.out.println("May");
  break;
  case 6:
  System.out.println("June");
  break;
  case 7:
  System.out.println("July");
  break;
  case 8:
  System.out.println("August");
  break;
  case 9:
  System.out.println("September");
  break;
  case 10:
  System.out.println("October");
  break;
  case 11:
  System.out.println("November");
  break;
  case 12:
  System.out.println("December");
  break;
  default:
  System.out.println("Enter number between 1 to 12");
  break;
 }
}
}

Output:

Enter a number
7
July
Share:

Our Services

Search This Blog

  • ()
Powered by Blogger.

GBWhatsApp APK Download

Your download will start automatically ..... if not  Click Here