Thread: Palindromes

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    Palindromes

    I have to write a program that tells you whether or not a number is a palindrome - I'm not asking for the program itself, but I can't even think of a way to start. We are only allowed to use while statements and use math.h/stdio.h.


    Thanks for any help in advance!

  2. #2
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    If the number is an integer you could extract a single digit using modulo and division.
    If we have the number 123 then:
    (123 % 1000) / 100 == 1
    (123 % 100) / 10 == 2
    (123 % 10) / 1 == 3

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    Maybe I'm just not seeing it right, but I don't quite understand how that would work for every possible input. I would have to make a different while statement for each additional digit wouldn't I?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    There has to be some way to tell if it's a palindrome without separating the numbers; that's the problem I'm running across. Otherwise I would have to set an infinite number of parameters...

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    In case you guys didn't know, a palindrome number is a number that reads the same both ways. 12321 is an example.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We know what a palindrome is; we were worried you didn't.

    Why do you think you would need an infinite number of parameters? One, there's only ten digits; two, equal vs. not-equal is a pretty obvious distinction that doesn't require you to differentiate between the possible digits anyway.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    Look; I'm really new to programming. I'm not looking for a free ride on my assignment and I'm not trying to insult anyone's intelligence. I forgot about the limits on how many numbers C can understand; like I said, I'm really new to programming. Sorry if I offended you in any way.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    sprintf is in stdio.h, does that count?
    The problem is very easy if you create a string version of it.

    Or consider
    while ( (x/=10) > 0 ) leftpower *= 10;
    as a means of working out what power of 10 the left-most digit in your input number is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    I actually figured out how to do it using the method the first guy suggested. It's a little lengthy but i don't know how to use sprintf heh. Thanks a lot though!
    Last edited by jestervr; 09-28-2008 at 01:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindromes
    By jewelz in forum C++ Programming
    Replies: 57
    Last Post: 02-23-2009, 06:11 PM
  2. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  3. palindromes....
    By imbecile in C in forum C Programming
    Replies: 8
    Last Post: 08-09-2003, 05:08 PM
  4. Palindromes
    By cman in forum C Programming
    Replies: 1
    Last Post: 05-05-2003, 06:39 AM
  5. strings and palindrome's
    By watshamacalit in forum C Programming
    Replies: 6
    Last Post: 01-07-2003, 06:17 PM