Thread: Print first N palindrome numbers

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    2

    Print first N palindrome numbers

    Hello, I have this program:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int N, i, j, reversedNumber, currentNumber, remainder;
    	
    	printf("Give N numbers: ");
    	scanf("%d", &N);
    	
    	
    	for (i = 0; i <= N; i++)
    	{
    		for (j = 0; j <= i; j++)
    		{
    			remainder = currentNumber % 10;
                currentNumber = currentNumber / 10;
                reversedNumber = reversedNumber * 10 + remainder;
    		}
    		
    		if (reversedNumber == N)
    		{
    			printf("%d", N);
    			i++;
    		}
    	}
    	return 0;
    }
    What I am trying to do is to make it print the first n palindrome numbers(not until n but the first n numbers). The problem is that the program exists and it doesn't print nothing. Any suggestion?

    Thank you for your time

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    Put aside this program and instead write a program that prints "yes" if the number input is a palindrome number and "no" otherwise. Show us this program.

    The idea is that first you need confidence to know that your palindrome number testing is working correctly. Then, you can more confidently identify and debug the logic error in your current program because you can rule out an issue with the palindrome number testing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    2
    So, I made this:

    Code:
    #include <stdio.h>
    
    
    int main() 
    {
        int N, reverse = 0, rem, N1;
        
        printf("Enter an integer: ");
        scanf("%d", &N);
        N1 = N;
    
    
        while (N != 0) 
        {
            rem = N % 10;
            reverse = reverse * 10 + rem;
            N /= 10;
        }
    
    
        if (N1 == reverse)
            printf("%d palindrome", N1);
        else
            printf("%d not palindrome", N1);
        return 0;
    }
    What I found out is that in order to check the number I have to save it in another variable and do the check with that variable not the N itself because I always get 0 as return. Which if I am right it happens from the calculations made inside the while. Other than that I couldn't find something else

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to print the sum of N numbers?
    By keivi in forum C Programming
    Replies: 16
    Last Post: 09-18-2013, 10:14 AM
  2. Print the 2 greater numbers of 10 numbers.
    By joelro79 in forum C Programming
    Replies: 6
    Last Post: 01-20-2012, 04:53 AM
  3. Palindrome checker for numbers
    By Ákos Kovács in forum C Programming
    Replies: 3
    Last Post: 07-27-2011, 02:31 PM
  4. Need help to print even numbers
    By ortegac in forum C Programming
    Replies: 3
    Last Post: 05-21-2006, 12:01 AM
  5. How to print numbers?????
    By AssistMe in forum C Programming
    Replies: 2
    Last Post: 03-04-2005, 08:22 AM

Tags for this Thread