Thread: please help with palindrome

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    5

    please help with palindrome

    I am in the beginning stages of C programming and in need of help.
    I can't understand. please can someone explain me?

    Why we assign reverse to 0 at first and what does the statements mean in the while statement? I also don't understand why temp=n...

    Code:
    #include <stdio.h>
     
    main()
    {
       int n, reverse = 0, temp;
     
       printf("Enter a number to check if it is a palindrome or not\n");
       scanf("%d",&n);
     
       temp = n;
     
       while (temp != 0)
       {
          reverse = reverse * 10;
          reverse = reverse + temp%10;
          temp    = temp/10;
       }
     
       if (n == reverse)
          printf("%d is a palindrome number.\n", n);
       else
          printf("%d is not a palindrome number.\n", n);
     
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    Hey,

    Tried to comment lines. Hope it'll be helpfull.

    Code:
    #include <stdio.h>
     
    main()
    {
       int n, reverse = 0, temp; // Initialize reverse at zero (if not so it has a garbage value) because we use it to calculate new value. We don't want to use any valued variable. 
     
       printf("Enter a number to check if it is a palindrome or not\n");
       scanf("%d",&n);
     
       temp = n; // Hold taken number to process it. We don't want to lose real value because we need to use it to check if it is palindrome.
     
       while (temp != 0) // Loop for calculation. 'temp' will be parsed/reversed.
       {
          reverse = reverse * 10; // Shift left by one
          reverse = reverse + temp%10; // Take the most value of temp and add it to reverse's least.
          temp    = temp/10; // Shift right the temp by one.
       }
     
       if (n == reverse) // Checking :)
          printf("%d is a palindrome number.\n", n);
       else
          printf("%d is not a palindrome number.\n", n);
     
       return 0;
    }

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    You can do away with the temp variable if you simply write a function called (for example) unsigned reverse_digits(unsigned n);

    Also, please declare main as int main(void) (i.e. be explicit)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome
    By jturner38 in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2010, 09:42 PM
  2. Palindrome
    By jturner38 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2010, 06:51 AM
  3. Palindrome in C
    By DaniiChris in forum C Programming
    Replies: 13
    Last Post: 07-24-2008, 11:32 PM
  4. palindrome
    By nightingale in forum C Programming
    Replies: 2
    Last Post: 07-26-2003, 02:45 PM
  5. Palindrome
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 09:03 AM