Fault in execution of the below given code

Can someone help me with this? Apparently, my first length function loop is running infinitely but I'm not able to understand the fault (that's what I understood by trying to debug it). Can someone rectify my mistake if any? I've tried calling by address in the palindrome function and by value in the length function (I just wanted to use a self made length function too)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
#include <bits/stdc++.h>
using namespace std;

int length(int n)
{
    int i=0;
    while(n>0)
    {
        n = n%10;
        i++;
    }
    return i;
}
void pali(int *p)
{
    int n,i,flag = 0;
    n = length(*p);
    for(i=1;i<=n/2;i++)
    {
        if((*p)/pow(10,i) == (*p)/pow(10,n-i))
        {
            flag++;
        }
        else
        {
            cout<<"It is not a palindrome!";
            break;
        }
    }
    if(flag)
        cout<<"It is a palindrome :)";
}
int main()
{
    int num;
    cout<<"Enter the number:";
    cin>>num;
    pali(&num);
}
> if((*p)/pow(10,i) == (*p)/pow(10,n-i))
1. Comparing floating point numbers for equality is a bad idea.
https://c-faq.com/fp/fpequal.html

2. pow() might give you answers like 100000.00001 or 999999.999999

Try recoding your approach using only integer arithmetic.
Say by creating your own version of pow() that does only integers.
Using this method (it's easier to first convert an int to a string), then possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

int length(int n) {
	int i {};

	while (n > 0) {
		n /= 10;
		++i;
	}

	return i;
}

void pali(int* p) {
	for (int i { 1 }, n { length(*p) }; i <= n / 2; ++i)
		if ((*p / static_cast<int>(pow(10, i - 1))) % 10 != (*p / static_cast<int>(pow(10, n - i))) % 10) {
			std::cout << "It is not a palindrome!\n";
			return;
		}

	std::cout << "It is a palindrome :)\n";
}

int main() {
	int num {};

	std::cout << "Enter the number:";
	std::cin >> num;

	pali(&num);
}


NB, I suggest you revise % and integer /

Also, why pass p as a pointer in pali()??
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <iterator>

void pali(const std::string& num) {
	auto b { num.begin() };
	auto e { num.rbegin() };

	for (; std::distance(b, e.base()) > 0; ++b, ++e)
		if (*b != *e) {
			std::cout << "Is not a palindrome\n";
			return;
		}

	std::cout << "Is a palindrome\n";
}

int main() {
	int num {};

	std::cout << "Enter the number:";
	std::cin >> num;

	pali(std::to_string(num));
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

bool isPalindrome( unsigned long long n )
{
   unsigned long long power10 = 1;
   for ( unsigned long long j = 10; j <= n; j *= 10 ) power10 = j;
   
   for ( ; power10 >= 10; power10 /= 100 )
   {
      if ( n / power10 != n % 10 ) return false;
      n %= power10;   n /= 10;
   }  
   return true;
}

int main()
{
   unsigned long long n;
   cout << "Enter the number: ";   cin >> n;
   cout << n << ( isPalindrome( n ) ? " is " : " is not " ) << "a decimal palindrome\n";
}
Last edited on
its worth your time to write an integer power function anyway, both int to int power and double to int power. Its faster than pow & solves the issues listed. A good start to a personal tool library.

(int)log10(number)+1 is its length. or, you can round up the log to next decimal (is that ceil? I rarely use those). anyway its a one-liner though you don't need it explicitly here (shown above).

this seems like a problem with a cute solution... hmm..
Topic archived. No new replies allowed.