Thread: code to reverse a 4=digit number+finding if 4 digit number is a palindrome

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    code to reverse a 4=digit number+finding if 4 digit number is a palindrome

    Hi everyone,
    Just started learning C.I worked on the first part and wrote a code that finds the reverse of the 4 digit number.But i am unsure as to what to do about the palindrome thing.I have to use only if-else statements.Please help me out with this one.Thanks!!
    Code:
    # include<stdio.h>
    int main()
    {
          int n;
          printf("Enter a four-digit number:");
          scanf("%d",&n);
          
          printf("The reversed four digit number is %d,%d,%d,%d \n",(n%10),(n/10)%10,(n/100)%10,n/1000);
          return (0);
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You have the first part. Now store the last two numbers like you did the reversal in the printf statement in a variable, then store the first two. If they are equal you have a palindrome.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Just reverse the number and compare it to itself. You can do that using strings, or more elegantly using integers.

    In my article "Finding Numbers That Are Palindromic In Multiple Bases", I have a C function called isPalindrome that does the check using integers (it uses a while loop though).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 04:26 AM
  2. finding no of digit
    By vajira in forum C Programming
    Replies: 3
    Last Post: 06-05-2003, 02:39 PM
  3. Finding digits in a number
    By MadStrum! in forum C Programming
    Replies: 5
    Last Post: 09-05-2002, 03:05 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM