Java Number Palindrom

瘦欲@ 提交于 2019-12-11 08:31:53

问题


I want to write a palindrome program which will print all palindrome numbers created by multiplying two-digit numbers (10-99)?

Here is my code so far:

public class PrintPalindrom {
    public int printPalindrom (int a, int b) {
        int result = a*b;
        int reverse = 0;
        if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
            while (result != 0) {
                reverse = reverse * 10;
                reverse = reverse + result % 10;
                result = result/10;
                System.out.println("palindrom is " + result);
            }
        } else {
            System.out.println("Wrong numbers");
        }
        return result;
    }
}

How to pirnt palindrome numbers created by multiplying two-digit numbers (10-99)?

My method dosent work for now...


回答1:


Create a method (isPalindrome()) that returns true if a number is palindrome, false otherwise. You can easily create it starting from your printPalindrom() method. Then, in your main():

int from = 10;
int to = 99;
for (int i = from; i <= to; i++) {
    for (int j = from ; j <= to; j++) {
        int mult = i*j;
        if (isPalindrome(mult))
            System.out.println(i + "x" + j + "=" + mult + " is plaindrome!");
    }
}



回答2:


Please check with this code:

 public class PrintPalindrom {
    public int printPalindrom (int a, int b) {
        int result = a*b;
        int temp = result; 
        int reverse = 0;
        if (a >= 10 &&  a <= 99 && b >= 10 && b <= 99) {
            while (temp != 0) {
                reverse = reverse * 10;
                reverse = reverse + result % 10;
                temp = temp/10;                
            }
           if(result == reverse) {            
            return result;   
           } 
       }
      return 0;     

    }   

     int from = 10;
     int to = 99;
     for (int i = from; i < to; i++) {
         for (int j = i + 1 ; j <= to; j++) {
              int result = printPalindrom(i, j);
              if(result > 0) {
                 System.out.println(result + ",");
             }              
        }
    }
}



回答3:


You could try this, using a stack if allowed to check whether a product of two numbers is a palindrom or not.

import java.util.Stack;

public class PrintPalindrom {
    public static void main(final String[] args) {
        final int MIN = 10;
        final int MAX = 99;
        for (int i = MIN; i <= MAX; i++) {
            for (int j = MIN ; j <= MAX; j++) {
                new PrintPalindrom(i, j);
            }
        }
    }

    public PrintPalindrom(final int a, final int b) {
        if (a >= 10 &&  a <= 99 && b >= 10 && b <= 99) {
            final String result = String.valueOf(a * b);    //Convert the product of the int to a String
            final Stack<String> stack = new Stack<String>();

            System.out.print(a + " * " + b + " = " + result + "\t-\t");

            //Push each character from the in to a stack
            for(int n = 0; n < result.length(); n++) {
                stack.push(result.substring(n, n + 1));
            }

            //Check each character against the characters in the stack
            for(int n = 0; n < result.length(); n++) {
                if(!result.substring(n, n + 1).equals(stack.pop())) {
                    System.out.println("Not a palindrom");
                    return;
                }
            }
        }

        System.out.println("A palindrom");
    }
}



回答4:


public class Example {

    public static void main (String[]args){
        int start = 10;
        int end = 99;            
        for (int i = start; i < end; i++) {
            for (int j = i ; j <= end; j++) {
                 printPali(i,j);              
           }
        }
    }

    public static void printPali(int i, int j) {            
        if(String.valueOf(i*j).equals(new StringBuilder(String.valueOf(i*j)).reverse().toString()))
            System.out.println(i + " * " + j + " = " + i*j);
    }
}



回答5:


you can achieve that in this way Create isPalindrome function which return true if the number is Palindrome then check Palindrome for each multiply result in the desired range and print it if it's true.

 public static boolean isPalindrome(int num){
              int rnum = 0,digit;
              int n = num;
              while(n > 0){
                    digit = n % 10;
                    n = n / 10;
                    rnum = rnum * 10  + digit;
              }
              if(rnum == num){
                  return true;
               }
               else{
                  return false;
               }
 }        
 public static void main(String[] args) {          
            int a = 10 , b = 99;
        for(int i = a; i <= b; i++){
            for(int j = i; j <= b; j++){
                 if(isPalindrome(i * j)){
                     System.out.println("Mult Palindrome: " + i * j);
                 }
            }
        }   
 }  

note that you can put range in the inner for loop for(int j = a; j <= b; j++) but it will be redundant in the multiply results without benefit so it's more efficient and optimized way to put the range from j=i to b.

I hope this will help.




回答6:


import java.util.Scanner;

public class Javatips {

    public static void main(String[] args){

        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        int reverseNumber = 0;
        int copyNumber = number;
        boolean isPalindrom = true;
        while (copyNumber>0){
            reverseNumber = 10*reverseNumber + copyNumber%10;
            copyNumber=copyNumber/10;
            if(reverseNumber==number){
                isPalindrom = true;
            } else {
                isPalindrom = false;
            }
        }
        if(isPalindrom){
            System.out.println(number + " is palindrom");
        } else {
            System.out.println(number + " is not palindrom");
        }
    }
}


来源:https://stackoverflow.com/questions/43043810/java-number-palindrom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!