In: Computer Science
Create using Java
Description: Palindrome -- According to
wikipedia "A palindrome is a word, phrase, number
or other sequence of units that can be read the same way in either
direction"
Write a application that can determine if a 5 digit number you
input is a palindrome. If the number is a palindrome then print
"The number is a palindrome." If it is not then print "The number
is NOT a palindrome"
Make sure to use an array
//-----------------------------------
// This is the good version pseudocode
// create string sInput
//
// prompt for input
//
// create char array (cArray) and assign
sInput.toCharArray()
//
// loop to check for palindrome (set i = 0, j =
sInput.length()-1, check to see if i != j; increment i, decrement
j)
// check to see if current array
values are !=
// print not a
palindrome
// return
// end of loop
// print is a palindrome
//------------------------------
input code:
output:
code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
/*declare the variables*/
Scanner sc=new
Scanner(System.in);
/*take input from user*/
System.out.print("Enter the String:
");
String sInput=sc.next();
/*declare the Character
array*/
char cArray[]=new
char[sInput.length()];
/*assign value into the Character
array*/
for(int
i=0;i<sInput.length();i++)
{
cArray[i]=sInput.charAt(i);
}
int j=sInput.length(),count=0;
/*check if string is Palindrome or not*/
for(int i=0;i<sInput.length();i++)
{
/*if not match than break the loop*/
if(cArray[i]!=cArray[j-i-1])
{
count=1;
break;
}
}
/*print according to the counter*/
if(count==0)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
}