开发者

Print Palindrome in Java

开发者 https://www.devze.com 2023-01-10 07:54 出处:网络
I have the following code and i want to print the palindrome on console. Please let me know what should come inside the if condition so that it can print all palindrome in between 0 to 10000.

I have the following code and i want to print the palindrome on console. Please let me know what should come inside the if condition so that it can print all palindrome in between 0 to 10000.

The palindrome is 161, 1221, 4554, etc....

Java code:

int palindrome[];
palindrome = new int[10000];

for (int count = 0; count < palindrome.length; count++ ){
    palindrome[count] = 0;
    if(){
        System.开发者_开发知识库out.println(count);
    }
}


This will do:

    if (isPalindrome(count)) {
        System.out.println(count);
    }

...

public boolean isPalindrome(int num) {
    // implement method here (ie: do your homework)
}


Something like this might work

public boolean isPalindrome(final int num) {
    final String s = Integer.toString(num);
    return s.equals(new StringBuffer(s).reverse().toString());
}


You need to transform the number into a string and then check if the string reversed is equal to the original string. This looks a lot like some homework, if so please add the appropriate tag.


The code golf solution:

   public static boolean isPalindrome(int number) {
       return ("" + number).equals(new StringBuilder("" + number).reverse().toString());
   }


You could also reverse the number and check if the reversed one and your number are equal.

To reverse the number: keep dividing it with 10 until the result is 0. Save the remainders of the divisions. Starting from the first remainder you get, multiply it by 10 and add the next remainder. Keep multiplying by 10 and adding remainders till you use all the remainders.


To revers I use a StringBuffer, StringBuffer(num).reverse().toString();


Here are some hints:

a) To get the last digit of a number use the remainder (%) operator

Example:
result = number % 10; // 21 % 10 = 1

b) To cut of the last digit, divide by 10

Example:
number = number / 10; // 21 / 10 = 2;

c) Multiply the result by 10

Example:
result = result * 10; // 1 * 10 = 10

d) Repeat until some condition is fulfilled (shouldn't be too hard to figure out ;-))


boolean isPalin(int n)
{
 String s = Integer.toString(n);
 return s.equals( new StringBuffer(s).reverse().toString() );
}

so your if condition should contain: isPalin(count)


boolean isPalin(int n)
{
 int nn = 0, tmp = n;
 while(n > 0)
 {
    nn = nn*10 + n%10;
    n /= 10;
 }
 return tmp==nn;
}

I prefer doing it this way instead of using strings, since strings make the process slow as it involves a lot of overhead.


import java.util.*;
import java.io.*;

public class TestFinal {
    public static Scanner c = new Scanner(System.in);

    public static void main(String[] args) {
        String original ,reverse ="";
        // TODO, add your application code
        System.out.println("Enter the text!");
        original = c.next();
        int length = original.length();

        for(int i =length - 1;i >= 0;i--)
            reverse = reverse +original.charAt(i);
            if(original.equals(reverse))
                System.out.println("entered strig is a palindrome");
            else
                System.out.println("the text is not a palindrome");
    }
}


An easy way to print palindrome:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        String A=sc.next();

        /* Enter your code here. Print output to STDOUT. */

        int len = A.length();
        String B ="";
        for(int i=len-1;i>=0;i--){
            B=B+A.charAt(i);
        }

        if(A.equals(B))
            System.out.println("Yes");
        else
            System.out.println("No");

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消