Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: [Solved] Palindrome

  1. #1
    Junior Member satyarlenka's Avatar
    Join Date
    May 2013
    Location
    Bhubaneswar IND
    Posts
    7
    My Mood
    Inspired
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question [Solved] Palindrome

    It has been a few weeks for me in Java. Yesterday I was trying to write a program to check if a given number is palindrome or not. I was able to write a program in which it asks the user to input the number of digits. It ran okay without any errors.
    public class PalindromeNumberCheck {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		System.out.println("Enter the number to be checked : ");
    		Scanner input = new Scanner(System.in);
    		int num = input.nextInt();
    		int numOriginal = num;
    		System.out.println("Enter the number of digits : ");
    		int n = input.nextInt();
    		int reverse = 0;
    		while(n != 0) {
    			reverse = reverse + (num%10) * (int)Math.pow(10,(n-1));
    			num = num / 10 ;
    			n--;
    		}
    		System.out.println("reverse = "+reverse);
    		if(reverse == numOriginal)
    			System.out.println("The number is a palindrome !");
    		else
    			System.out.println("The number is not a palindrome !");
    	}
     
    }
    My question is, Is there any other way to do it without asking the user to input the number of digits? Kindly someone point out the logic and I will try out to write the program by myself.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: [HELP] Palindrome

    You mean you want to use the program's logic to figure out how many digits the user entered, right?

    There are a number of ways. The simplest that occurs to me is to first accept the number as a String using input.nextLine(), then use a String method to determine its length, then parse the String object to an integer using Integer.parseInt( inputString ).

    If there are elements of that suggestion you can't or won't use for whatever reason, then you could divide the number entered as an int by 10 in a loop until the value is < 10 and use a counter to determine how many digits it has.

    Will either of those suggestions work for you?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    satyarlenka (October 14th, 2013)

  4. #3
    Junior Member satyarlenka's Avatar
    Join Date
    May 2013
    Location
    Bhubaneswar IND
    Posts
    7
    My Mood
    Inspired
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Palindrome

    Thanks Greg!! Oh my god. That's easy. It never really stuck my mind. Especially the second one.(facepalm)

Similar Threads

  1. Palindrome
    By zorey02 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2013, 09:25 AM
  2. Testing for a Palindrome
    By ryan.sampson in forum Algorithms & Recursion
    Replies: 12
    Last Post: November 6th, 2011, 08:01 PM
  3. Palindrome program help
    By timm1371 in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 13th, 2011, 09:31 AM
  4. Palindrome
    By mag12203 in forum Algorithms & Recursion
    Replies: 13
    Last Post: December 20th, 2010, 08:28 PM
  5. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 03:05 AM

Tags for this Thread