1.Palindrome program in java using for loop
- package com.instaceofjava;
- public class PalindromeDemo{
- public static void main(String[] args) {
- String str="MADAM";
- String revstring="";
- for(int i=str.length()-1;i>=0;--i){
- revstring +=str.charAt(i);
- }
- System.out.println(revstring);
- if(revstring.equalsIgnoreCase(str)){
- System.out.println("The string is Palindrome");
- }
- else{
- System.out.println("Not Palindrome");
- }
- }
- }
Output:
- The string is Palindrome
2.palindrome program in java using stringbuffer
- package com.instaceofjava;
- import java.util.Scanner;
- public class Palindrome {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- System.out.println("Enter a string");
- String str=in.nextLine();
- StringBuffer strone=new StringBuffer(str);
- StringBuffer strtwo=new StringBuffer(strone);
- strone.reverse();
- System.out.println("Orginal String ="+strtwo);
- System.out.println("After Reverse ="+strone);
- if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
- System.out.println("Result:Palindrome");
- else
- System.out.println("Result:Not Palindrome");
- }
- }
Output:
- Enter a string
- MOOM
- Orginal String =MOOM
- After Reverse =MOOM
- Result:Palindrome
Simple palindrome program in java:
3.Program to Check given number is palindrome or not
- package com.instaceofjava;
- import java.util.Scanner;
- public class Palindrome {
- public static void main(String[] args)
- {
- System.out.println("Please Enter a number : ");
- int givennumber = new Scanner(System.in).nextInt();
- int number=givennumber;
- int reverse=0;
- while (number != 0) {
- int remainder = number % 10;
- reverse = reverse * 10 + remainder;
- number = number / 10;
- }
- if(givennumber == reverse)
- System.out.println("Result:Palindrome");
- else
- System.out.println("Result:Not Palindrome");
- }
- }
Output:
- Please Enter a number :
- 535
- Result:Palindrome
Java programming interview questions
- Print prime numbers?
- Dynamic polymorphism in Java
- Find missing numbers in an array
- Collection vs Collections
- Custom iterator in java
- Unreachable Blocks in java
- Get table cell data using JavaScript
- Enum in java
- 3 different ways to print exception message in java
- Can we have try without catch block in java
- toString() method in java with example program
- Remove duplicates from arraylist without using collections
- Explain Hibernate Configuration file
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java interview questions on exception handling
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
public static void isPalindrome(String test) {
ReplyDeletefor(int i = 0; i < test.length()/2; i++) {
if(test.charAt(i) != test.charAt(test.length() - (i + 1))) {
System.out.println("false");
return;
}
}
System.out.println("true");
}
how to remove duplicate character from String
ReplyDeleteString s="aabbccdd";
output sholud be=abcd
To consider the spaces in between.i.e. "Put it on" string is also a palindrome, little enhancement to the above code. just replace line no 12
ReplyDelete"String str=in.nextLine();" with
String str=in.nextLine().replace(" ", "");