寻找回文及其二进制

时间:2014-03-21 02:50:37

标签: java

我试图找到回文及其二进制文件,现在我正确得到了回文数,但它的二进制文件没有正确打印,请帮助我完成了我的错误。

     package Palindrome
     public class PalindromeNnumber {

     public static void main(String[] args) {

     for (int i = 11; i < 100; i++) {
     isPalindrome(i);
       }
     }
     private static void isPalindrome(int i) {
     int reverse = 0;
     int input = i;
     while (i > 0) {
     int remainder = i % 10;
     reverse = reverse * 10 + remainder;
     i = i / 10;
     }
     int rnumber = reverse;
     if (input == reverse) {
     System.out.println("Palindrome Number " + reverse);
     isBinary(rnumber);
     }
    }

   private static void isBinary(int rnumber) {
    int reversebinary = 0;

    //Finding Binary Number for the input number

    while (rnumber > 0) {

   int remainderb = rnumber % 2;

   reversebinary = reversebinary * 10 + remainderb;

   rnumber = rnumber / 10;
   }
  System.out.println("Binary Numbers are :"+reversebinary);
  }
 }

我的输出:

Palindrome Number 11
Binary Numbers are :1101
Palindrome Number 22
Binary Numbers are :1101
Palindrome Number 33
Binary Numbers are :100001
Palindrome Number 44
Binary Numbers are :1101
Palindrome Number 55
Binary Numbers are :111011
Palindrome Number 66
Binary Numbers are :100001
Palindrome Number 77
Binary Numbers are :1011001
Palindrome Number 88
Binary Numbers are :1101
Palindrome Number 99
Binary Numbers are :1100011

1 个答案:

答案 0 :(得分:2)

您想使用:

Integer.toBinaryString(int i)