I need to make a program to check if the entered number is palindrome or not i.e. if u take a number and reverse it it will be the same old number for example if u take 121..then if u reverse the number it will be 121 and so 121 is a palindrome number.

i wrote a program to check if a number is palindrome or not but it is not working properly :cry: ...Can any1 plz tell me wat is wrong..

The program which I wrote is below:-

#include<iostream.h>
#include<conio.h>

void main()

{
	
	int n, x, m=0;

	cout<<"Enter any number"<<endl;
	cin>>n;

	while(n>0)

	{

		m*=10;

		x=n%10;

		m+=x;

		n/=10;

	}

	if(m==n)

	cout<<"The entered number is a palindrome number"<<endl;

	else

	cout<<"The entered is not a palindrome number"<<endl;

	getch();

}

Dani AI

Generated

As pointed out, the original program reverses the digits by mutating n and then compares the reversed value to n — but n has been reduced to 0 by the loop. Preserving the input (use a saved copy like original) before the reversal fixes the logic. Also note the older snippet uses non-standard headers/functions (iostream.h, conio.h, void main, getch()); a portable, modern approach uses <iostream> and int main().

#include <iostream>
#include <limits>

int main() {
    long long original;
    if (!(std::cin >> original)) return 0;

    if (original < 0) {
        std::cout << "Negative numbers are not palindromes\n";
        return 0;
    }

    long long n = original;
    long long reversed = 0;
    while (n > 0) {
        int digit = n % 10;
        if (reversed > (std::numeric_limits<long long>::max() - digit) / 10) {
            std::cout << "Number too large to reverse safely\n";
            return 0;
        }
        reversed = reversed * 10 + digit;
        n /= 10;
    }

    std::cout << (reversed == original ? "palindrome\n" : "not a palindrome\n");
    return 0;
}

A couple of practical notes: use long long (or a string-based method) to avoid overflow on very large inputs; treat negative integers explicitly (often considered non-palindromes because of the minus sign); and remember that integer input loses any leading zeros — use a string comparison if leading zeros or arbitrarily long digit sequences must be preserved. The simple save-and-compare fix is what made ’s program work (and it’s the same correction later reiterated by ).

Recommended Answers

All 8 Replies

>if(m==n)
n is zero at this point, remember? Use a copy of n for your loop counter so that the original value is preserved.

Hey thanx a lot Narue...my program is working Perfectly now!! :cheesy:

And once again Thanx a lot....

Just a helpful hint here on the forum for you:

Cn u plz not use chat spk here? Wat do u want 2 do that for? U got all d time in d world 2 make a post, so there's no need 2 uz chat spk n here.

Please, for all of our eyes' sakes, spend about 30 seconds more on your post, and spell words in a non-lame fashion.

kthxbye! :D

tx alx, i culdnt ha sd it b8ter mself

Your program is write up to if(m==n)

in the above program n will become zero
after the loop. So assign n value to any other variable,then compare with m.

Congratulations, muraliaa! It took you only four years to come up with the same answer as me.

This thread is so old, it saw the creation of the previous LHC :)

commented: haha :)! +4

Congratulations, muraliaa! It took you only four years to come up with the same answer as me.

Some people are just a little slow :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.