Results 1 to 3 of 3
  1. #1
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default How to know if an integer is a palindrome?

    hi guys. I'm stuck on some coding.

    Without using strings and array, i am trying to reverse the number so i can test if it is a palindrome number( which means when an integer is in reversed order, it is equal to the original order)

    what i have so far:

    Java Code:
    public static int getPal(int num){
    	
    	int i;
    	int digit;
    	int rem;
    	double sum=0;
    	double length=0;
    	double n;
    	
    	length= findLength(num);
    	
    	for (i=0;i<num; i++){
    		
    		if (i<10){
    			System.out.println(""+i);
    		}
    		
    			
    	}
    	
    	
    	while (num>10){
    		n=num;
    		while (n>0){
    		rem=x%10;
    		sum=rem*length+sum;
    		length= length/10;
    		n=n/10;
    		}
    		
    		if (num ==sum) {
    			System.out.println(""+n);
    				
    		}
    		
    		num--;
    			
    	}
    return 0;
    }
    
    public static double findLength(int num){
    	double length=0;
    	
    	while (num>0){
    		num= num/10;
    		length++;
    	}
    	length= Math.pow(10,length-1);
    	
    return length;
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How to know if an integer is a palindrome?

    Does the program do what you expect? If not describe both the expected and actual behaviour when you give it some specific input. If it does not compile post the exact and entire compiler message and say which line(s) of your code it is referring to.

    -----

    Perhaps you could also describe *how* you are deciding whether the input is a palindrome. In particular the println() statements seem a little mysterious. It is important to have a (comprehensive and specific) plan before rushing into code.

  3. #3
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: How to know if an integer is a palindrome?

    Okay, i fixed it. The problem was the while loop and me using doubles instead of ints.

    Java Code:
    public static int getPal(int num){
    	
    	int i;
    	int digit;
    	int rem;
    	int x;
    	int sum;
    	int length=0;
    	int n;
    	
    	System.out.println("");
    	
    	for (i=0;i<num; i++){
    		
    		if (i<10){
    			System.out.println(""+i);
    		}
    		
    			
    	}
    	
    	
    	while (num>10){
    		n=num;
    		sum=0;
    		length= findLength(num);
    		while (n>0){
    		rem=n%10;
    		sum=rem*length+sum;
    		length= length/10;
    		n=n/10;
    		
    			if (num ==sum) {
    				System.out.println(""+sum);
    				
    			}
    		}
    		
    		num--;
    		
    	}
    return 0;
    }
    
    public static int findLength(int num){
    	double length=0;
    	int i=1;
    	
    	while (num>10){
    		num= num/10;
    		length++;
    	}
    	
    	
    	while (length>0){
    		i = i*10;
    		length--;
    	}
    	
    return i;
    }

Similar Threads

  1. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 09:59 PM
  2. palindrome
    By rochellelising in forum New To Java
    Replies: 1
    Last Post: 08-24-2011, 02:03 PM
  3. Palindrome
    By leepikamukharji in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 04:20 PM
  4. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM
  5. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 04:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •