Can someone help me rewrite the Palindrome without arrays. I want my code to exe
ID: 3636990 • Letter: C
Question
Can someone help me rewrite the Palindrome without arrays.I want my code to execute the same, after you make any changes.
I can't figure out a better way of writing this code:
No bugs in the current code, and it executes perfectly. I just need another perspective on how to program this code differently to reach the same execution. Thank you.
import java.util.Scanner;
public class Palindrome
{
public static void main( String[] args )
{
int number,
digit1,
digit2,
digit4,
digit5,
digits;
number = 0;
digits = 0;
while( digits != 5 )
{
//create Scanner to obtain user input
Scanner kbinput = new Scanner( System.in );
System.out.print( "Enter a 5 digit integer: " );
number = kbinput.nextInt();
if ( number < 100000 )
{
if ( number > 9999 )
digits = 5;
else System.out.print("Error! Must be 5 digits! Try again." );
}
else System.out.print("Error! Must be 5 digits! Try again." );
}
digit1 = number/10000;
digit2 = number%10000/1000;
digit4 = number%10000%1000%100/10;
digit5 = number%10000%1000%100%10;
if ( digit1 == digit5 )
{
if ( digit2 == digit4 )
System.out.print( number+ " " + "is a palindrome!!!");
else System.out.print(number+ " " + "is not a palindrome!!!");
}
else System.out.print( number + " " + "this is not a palindrome!!!");
while ( digit1 != digit5 || digit2 != digit4)
{
Scanner kbinput1 = new Scanner(System.in);
System.out.print("Try again! Enter a five digit integer: ");
number = kbinput1.nextInt();
digit1 = number/10000;
digit2 = number%10000/1000;
digit4 = number%10000%1000%100/10;
digit5 = number%10000%1000%100%10;
if ( digit1 == digit5 )
{
if ( digit2 == digit4 )
System.out.print( number+ " " + "is a palindrome!!!");
else System.out.print(number+ " " + "is not a palindrome!!!");
}
else System.out.print( number + " " + "this is not a palindrome!!!");
}
}
}
Explanation / Answer
please rate - thanks
import java.util.*;
public class palindrome
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
String input;
int i,j;
System.out.println("enter 5 digit number:(blank line to stop)");
input=in.nextLine();
while(input.length()!=0)
{i=0;
j=input.length()-1;
while(input.charAt(i)==input.charAt(j) &&i<=j)
{i++;
j--;
}
if(i>j)
System.out.println("palindrome");
else
System.out.println("NOT a palindrome");
System.out.println("enter 5 digit number:(blank line to stop):");
input=in.nextLine();
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.