Is the sequence of number palindrome or not?

Asked By 10 points N/A Posted on -
qa-featured

I am a student and I was asked by my Instructor to create a palindrome program in java. The main function of this program is to let the user enter a sequence of number and the program will determine if the input is a palindrome or not.

Palindrome is a word or sequence of number that read the same forwards or backwards, but the program have to determine sequence of numbers only. Words are not accepted.  

In addition, If the input is a palindrome the program will return a message stating "x(sequence of number) is a palindrome or "x(sequence of number is not a palindrome).

example:

Enter a number:115511

(115511 when read forwards and backward is 115511)

115511 is a palindrome

Enter a number:12345

(12345 when read forwards and backwards is 54321)

12345 is not a palindrome

Thank for anyone who can help.

SHARE
Best Answer by Sheldon Ron
Best Answer
Best Answer
Answered By 0 points N/A #101935

Is the sequence of number palindrome or not?

qa-featured
Palindrome or not.
import java.io.* ;
public class pallindrome {
 
    public static void main( String args[]){
        InputStreamReader inputstream = new InputStreamReader( System.in ) ;
        BufferedReader buffer = new BufferedReader(inputstream) ;
        try {
             System.out.print( " Enter Text : ");
             String txt = buffer.readLine();     //Accept text.
             int len=txt.length();
             int i,j;
             for( i=0, j = len-1; i < len ; i++ , j–){        
                 if ( txt.charAt(i) != txt.charAt ( j )){     /*Compare last and first char*/
                     System.out.println ( " The Given Text is Not a Palindrome " );
                     return;
                 }
             }
             System.out.println( " The Given Text Is a Palindrome " );/*This is executed when the entire loop finds all the last and first characters are same.*/
        }
        catch (IOException err) {
             System.out.println ( " Error reading line " );
        }
    }
}
Let me Explain it.
Palindrome are words where the text read from left to right and right to left are same.
Suppose you have a text 12345, this is not a pallindrome.
However the text, 1234321 is a pallindrome.
The above program initializes the Input Stream Reader to read the text.
We have two pointers/ variables one to count from left to right and another from right to left.
The loop breaks if any character while going from left to right and while going from right to left finds a mismatch.
And if such a mismatch occurs, then you say that text is not pallindrome. If the loop completes to the end, then you confirm that the text is a pallindrome.
Answered By 0 points N/A #195462

Is the sequence of number palindrome or not?

qa-featured

 

Hi Samantha Smith,
 
Here is a simple code using string reverse function and a simple GUI.
 
import javax.swing.JOptionPane;
 
public class PalindromeCheck{
public static void main(String[]args){
String input=JOptionPane.showInputDialog(null,"Enter The word You want to check","Palindrome Check",JOptionPane.QUESTION_MESSAGE);//shows a new text box with following message "Enter The word You want to check" and title "Palindrome Check". The value entered will be stored to variableinput
if(input!=null){
String temp=input;
StringBuffer inputNew=new StringBuffer(temp);
if(input.equals(inputNew.reverse().toString())){//reversing user input string and comparing with the input.
JOptionPane.showMessageDialog(null,"Its a palindrome");
}else{
JOptionPane.showMessageDialog(null,"Its not a palindrome");
}
}
}
}
 
 
Hope it helps.

Login/Register to Answer

Related Questions