Skip to main content
Question Protected by Jamal
edited title
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Making a palindrome Palindrome program more efficientwith reverse method

deleted 395 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Making a palindrome program more efficient

I wrote a program for a college assignment, and I'd like to receive some pointers on making my code a little more efficient. It'd be much appreciated. I'm still new to coding, so I apologize if my code is horrendous.

package lab07;

import java.util.Scanner;
public class Lab07 {

public static int reverse(int number){
       
       int result = 0;
       while (number !=0){
           int remainder = number % 10;
           result = result * 10 + remainder;
           number = number / 10;
       }
       return result;
   }
   public static boolean isPalindrome(int input){
   
   int Palindrome = reverse(input);
   
   if (Palindrome == input){
       
       return true;          
   }
   else
       return false;
   }
    public static void main(String[] args) {
   
   int integer = 0;
   Scanner input = new Scanner(System.in);      
   System.out.print("Enter a positive, multi-digit integer: ");
   integer = input.nextInt();
   while (integer <= 9 && integer > 0)
   {
        System.out.println(integer + " is a single digit. Please re-enter another integer: ");
        integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is a palindrome");
                return;
           }
           else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is not a palindrome");
                return;
           }
         
   }
   while (integer < 0)
   {
       System.out.println(integer + " is negative. Please re-enter another integer: ");
       integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is a palindrome");
                 return;
            }
            else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is not a palindrome");
                 return;
            }
                 while (integer <= 9 && integer > 0)
                        {
                             System.out.println(integer + " is a single digit. Please re-enter another integer: ");
                             integer = input.nextInt();
                                     if (isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is a palindrome");
                                     return;
                                }
                                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is not a palindrome");
                                     return;
                                }
                        }                   
   }
   while (integer > 9){
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is a palindrome");
                     return;
                }
                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is not a palindrome");
                     return;
                }
   }
   
}
                                           
   }

Sorry I didn't specify this before but I'm in an Intro to Programming class, and our professor hasn't taught us how to reverse strings. These were his instructions:

The program should ask the end user to enter an integer. Use the isPalidrome isPalidrome method to invoke the reverse method, and report whether the the integer is a palindrome. Any one-digit integer or negative integer integer should be rejected with the specific error message (negative or or one digit), and then the program should ask the user to re-enter the the integer. Hint: To reverse the digits of a number, try this routine routine:

int result = 0;                           
while (number != 0) {                   // e.g. number = 123
                                        // Iteration 1        Iteration 2    Iteration 3 
  int remainder = number % 10;          // remainder = 3      remainder = 2  remainder = 1 
  result = result * 10 + remainder;     // result = 3         result = 32    result = 321
  number = number / 10;                 // number = 12        number = 1     number = 0
}                                       // result contains the reverse of number

My solution:

package lab07;

import java.util.Scanner;
public class Lab07 {

public static int reverse(int number){
       
       int result = 0;
       while (number !=0){
           int remainder = number % 10;
           result = result * 10 + remainder;
           number = number / 10;
       }
       return result;
   }
   public static boolean isPalindrome(int input){
   
   int Palindrome = reverse(input);
   
   if (Palindrome == input){
       
       return true;          
   }
   else
       return false;
   }
    public static void main(String[] args) {
   
   int integer = 0;
   Scanner input = new Scanner(System.in);      
   System.out.print("Enter a positive, multi-digit integer: ");
   integer = input.nextInt();
   while (numberinteger !<= 9 && integer > 0)
   {
        System.out.println(integer + " is a single digit. Please re-enter another integer: ");
        integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is //a epalindrome");
                return;
           }
           else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.gout.println(integer number+ =" 123is not a palindrome");
                return;
           }
          
   }
 // Iteration 1while (integer < 0)
   {
  Iteration 2    IterationSystem.out.println(integer 3+ " is negative. Please re-enter another integer: ");
  int remainder    integer = numberinput.nextInt();
 % 10;          // remainder = 3 if (isPalindrome(integer) && (integer > 0 && remainderinteger => 29))
  remainder = 1        {
  result = result * 10           System.out.println(integer + remainder;" is a palindrome");
  // result = 3         result = 32 return;
   result = 321       }
  number = number / 10;      else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
   // number = 12      {
  number = 1     number        System.out.println(integer + " is not a palindrome");
                 return;
            }
                 while (integer <= 9 && integer > 0)
                        {
                             System.out.println(integer + " is a single digit. Please re-enter another integer: ");
                             integer = input.nextInt();
                                     if (isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is a palindrome");
                                     return;
                                } 
                                else if (!isPalindrome(integer) && (integer > 0 //&& resultinteger contains> the9))
 reverse of number                             {
                                     System.out.println(integer + " is not a palindrome");
                                     return;
                                }
                        }                   
   }
   while (integer > 9){
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is a palindrome");
                     return;
                }
                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is not a palindrome");
                     return;
                }
   }
   
}
                                           
   }

He provided the reverse method for us, I wouldn't have know how to actually reverse the integer on my own to be honest. However I appreciate the help on determining the palindrome.

Making palindrome program more efficient

I wrote a program for a college assignment, and I'd like to receive some pointers on making my code a little more efficient. It'd be much appreciated. I'm still new to coding, so I apologize if my code is horrendous.

package lab07;

import java.util.Scanner;
public class Lab07 {

public static int reverse(int number){
       
       int result = 0;
       while (number !=0){
           int remainder = number % 10;
           result = result * 10 + remainder;
           number = number / 10;
       }
       return result;
   }
   public static boolean isPalindrome(int input){
   
   int Palindrome = reverse(input);
   
   if (Palindrome == input){
       
       return true;          
   }
   else
       return false;
   }
    public static void main(String[] args) {
   
   int integer = 0;
   Scanner input = new Scanner(System.in);      
   System.out.print("Enter a positive, multi-digit integer: ");
   integer = input.nextInt();
   while (integer <= 9 && integer > 0)
   {
        System.out.println(integer + " is a single digit. Please re-enter another integer: ");
        integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is a palindrome");
                return;
           }
           else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is not a palindrome");
                return;
           }
         
   }
   while (integer < 0)
   {
       System.out.println(integer + " is negative. Please re-enter another integer: ");
       integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is a palindrome");
                 return;
            }
            else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is not a palindrome");
                 return;
            }
                 while (integer <= 9 && integer > 0)
                        {
                             System.out.println(integer + " is a single digit. Please re-enter another integer: ");
                             integer = input.nextInt();
                                     if (isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is a palindrome");
                                     return;
                                }
                                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is not a palindrome");
                                     return;
                                }
                        }                   
   }
   while (integer > 9){
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is a palindrome");
                     return;
                }
                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is not a palindrome");
                     return;
                }
   }
   
}
                                           
   }

Sorry I didn't specify this before but I'm in an Intro to Programming class, and our professor hasn't taught us how to reverse strings. These were his instructions:

The program should ask the end user to enter an integer. Use the isPalidrome method to invoke the reverse method, and report whether the integer is a palindrome. Any one-digit integer or negative integer should be rejected with the specific error message (negative or one digit), and then the program should ask the user to re-enter the integer. Hint: To reverse the digits of a number, try this routine:

int result = 0;                           
while (number != 0) {                   // e.g. number = 123
                                        // Iteration 1        Iteration 2    Iteration 3 
  int remainder = number % 10;          // remainder = 3      remainder = 2  remainder = 1 
  result = result * 10 + remainder;     // result = 3         result = 32    result = 321
  number = number / 10;                 // number = 12        number = 1     number = 0
}                                       // result contains the reverse of number  

He provided the reverse method for us, I wouldn't have know how to actually reverse the integer on my own to be honest. However I appreciate the help on determining the palindrome.

Making a palindrome program more efficient

I wrote a program for a college assignment, and I'd like to receive some pointers on making my code a little more efficient.

The program should ask the end user to enter an integer. Use the isPalidrome method to invoke the reverse method, and report whether the integer is a palindrome. Any one-digit integer or negative integer should be rejected with the specific error message (negative or one digit), and then the program should ask the user to re-enter the integer. Hint: To reverse the digits of a number, try this routine:

int result = 0;                           
while (number != 0) {                   // e.g. number = 123
                                        // Iteration 1        Iteration 2    Iteration 3 
  int remainder = number % 10;          // remainder = 3      remainder = 2  remainder = 1 
  result = result * 10 + remainder;     // result = 3         result = 32    result = 321
  number = number / 10;                 // number = 12        number = 1     number = 0
}                                       // result contains the reverse of number

My solution:

package lab07;

import java.util.Scanner;
public class Lab07 {

public static int reverse(int number){
       
       int result = 0;
       while (number !=0){
           int remainder = number % 10;
           result = result * 10 + remainder;
           number = number / 10;
       }
       return result;
   }
   public static boolean isPalindrome(int input){
   
   int Palindrome = reverse(input);
   
   if (Palindrome == input){
       
       return true;          
   }
   else
       return false;
   }
    public static void main(String[] args) {
   
   int integer = 0;
   Scanner input = new Scanner(System.in);      
   System.out.print("Enter a positive, multi-digit integer: ");
   integer = input.nextInt();
   while (integer <= 9 && integer > 0)
   {
        System.out.println(integer + " is a single digit. Please re-enter another integer: ");
        integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is a palindrome");
                return;
           }
           else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is not a palindrome");
                return;
           }
          
   }
   while (integer < 0)
   {
       System.out.println(integer + " is negative. Please re-enter another integer: ");
       integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is a palindrome");
                 return;
            }
            else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is not a palindrome");
                 return;
            }
                 while (integer <= 9 && integer > 0)
                        {
                             System.out.println(integer + " is a single digit. Please re-enter another integer: ");
                             integer = input.nextInt();
                                     if (isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is a palindrome");
                                     return;
                                } 
                                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is not a palindrome");
                                     return;
                                }
                        }                   
   }
   while (integer > 9){
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is a palindrome");
                     return;
                }
                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is not a palindrome");
                     return;
                }
   }
   
}
                                           
   }
edited tags
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
added 1350 characters in body
Source Link
Gandalf
  • 163
  • 1
  • 1
  • 4
Loading
deleted 668 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
added 1 characters in body
Source Link
Gandalf
  • 163
  • 1
  • 1
  • 4
Loading
Source Link
Gandalf
  • 163
  • 1
  • 1
  • 4
Loading