0
int main(){
    int n, val, i,sum(0),temp,m(0);
    cout<<"Enter the number: ";
    cin>>n;
    temp=n;
    while(n>0){
        n/=10;
        m++;
    }
    n=temp;
    while(n>0){
        m--;
        i=n%10;
        val= i*pow(10,m);
        cout<<"Power: "<<val<<endl;
        sum=sum+val;
        n/=10;
    }
    if(temp==sum){
        cout<<"Palindrome";
    }
    else{
        cout<<"Not Palindrome";
    }
    return 0;
}

Here in this part:

        val= i*pow(10,m);
        cout<<"Power: "<<val<<endl;

If m=2 and i=1 then this should give 100 but I'm getting 99 as output. This is happening only for the first val and for the other it shows the perfect value

enter image description here

Can anyone tell me what I'm doing wrong

  • 1
    `pow` operates on floating-point types, and as a result are imprecise -- especially when truncating to integers. If you want to raise 10 to a power as an integer, just write your own function to do it. Alternatively, rearrange your math so that each time around the loop you _multiply_ the sum by 10 before adding a value, and you'll get the power function for free. – paddy Nov 29 '21 at 09:56
  • `pow(10, m)` use floating point (see [`std::pow`](https://en.cppreference.com/w/cpp/numeric/math/pow)). – Jarod42 Nov 29 '21 at 09:57
  • 1
    You might even get rid of power computation with something like `sum = 10 * sum + i;` – Jarod42 Nov 29 '21 at 10:01
  • [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – Evg Nov 29 '21 at 10:15
  • 3
    Don't use integer arithmetic to check number are palindromes. Numbers are just strings (of digits); read them as a `std::string` and then check to see if the string of characters is a palindrome or not. – Richard Critten Nov 29 '21 at 10:17
  • 2
    [Why does pow(5,2) become 24?](https://stackoverflow.com/q/22264236/995714), [Why the result of pow(10,2) 99 instead of 100?](https://stackoverflow.com/q/54057687/995714), [Why pow(10,5) = 9,999 in C++](https://stackoverflow.com/q/9704195/995714) – phuclv Nov 29 '21 at 10:17

2 Answers2

1

pow is a floating point function that if poorly implemented by the C++ standard library, can fail to return the best possible double even with an integral exponent. You might have better luck with one of the std::pow overloads but even then you're at the mercy of the quality of the C++ standard library.

Fortunately though you don't need pow at all!

In your first loop, where you set the starting value for m, do the same thing with val: setting it first to 1 then using val *= 10; in the loop body.

Then in the second loop apply val /= 10 as appropriate.

Take care not to overflow int: it can have a range as small as -32767 to +32767 (C++20 widens the lower limit to -32768). It might be an idea to use a long or a long long instead.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Just as a side not and unrelated to your "power" problem.

You can read the number as a string. Then you can compare this string with its reversed version. If equal, then you have a Palindrome else not. The standard approach is to use reverse iterators for that.

In the end, you will have only 3 statements in main. And it is not complicated . . .

Please see:

#include <iostream>
#include <string>

int main() {
    std::cout << "\nEnter a number: ";
    if (std::string number{}; std::cin >> number)
        std::cout << (number == std::string(number.rbegin(), number.rend()) ? "Palindrome" : "No palindrom") << '\n';
}

A M
  • 14,694
  • 5
  • 19
  • 44
  • Yes this is a good approach here, especially given that the overall program performance is I/O bound. Have an upvote! – Bathsheba Dec 02 '21 at 07:14