Program not giving output.

Q. WAP to find the next palindrome number larger than the input number.
for eg:- Input=25
Output=33
The program is giving correct output for number with all digits '9';
Please help. why is it not giving output.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int len,flag=1,count=0,num,ind;
    char ch,ch2;
    string st;
    string str;
    cout<<"Enter a number: ";
    cin>>str;
            len=str.length();
            for(int j=len-1;j>=0;j--)
            {
                if(str.at(j)!='9')
                {
                    count=1;
                    break;
                }
            }
            if(count==0)
            {
                for(int j=0;j<=len-2;j++)
                {
                    str.at(j)='0';
                }
                str.at(len-1)='1';
                str="1"+str;
                cout<<str;
                goto xyz;
            }
            while(flag!=0)
            {
                ind=len-1;
                num=(int)(str.at(ind))-48;
                num++;
                if(num==10)
                {
                    str.at(ind)='0';
                    ind=ind-1;
                    num=(int)(str.at(ind))-48;
                    num++;
                }
                ch2==(char)(num)+48;
                str.at(ind)=ch2;
                for(string::reverse_iterator r=str.rbegin();r<str.rend();r++)
                {
                    ch=(char)(*r);
                    st=st+ch;
                }
                if(st==str)
                {
                cout<<st;
                flag=0;
                }
            }
            xyz: ;
}

                    
                
                    
            
            
                
            
                
            
                

Last edited on
I would use a different structure:
1
2
3
4
5
6
7
8
9
10
11
12
bool isPalindrome( unsigned int );

int main() {
  unsigned int number = 0;
  std::cin >> number;
  ++number;
  while ( ! isPalindrome( number ) ) {
    ++number;
  }
  std::cout << number << '\n';
  return 0;
}

That leaves a predicate function for you to implement.

PS: Those magic -48 are creepy.
Let's make it readable:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Space and indent declarations: easier to read and understand what is happening.
#include <iostream>
#include <string>
//Ugly:
/*using namespace std;*/
//Add std before data structures

int main()
{
    /*int len,flag=1,count=0,num,ind;*/
    //Initialize all variables before
    int flag = 1, count = 0;
    /*char ch,ch2;*/
    //Ok, initialized by std >>
    
    std::string st;
    std::string str;
    
    std::cout << "Enter a number: ";
    std::cin >> str;
    
    int len = str.length();
    
    for(int j = len - 1; j >= 0; j--)
    {
        if(str.at(j) != '9')
        {
            count = 1;
            break;
        }
    }
    
    if(count == 0)
    {
        for(int j = 0; j<= len - 2; j++)
        {
            str.at(j) = '0';
        }
        
        str.at(len - 1) = '1';
        str = "1" + str;
        
        std::cout << str;
        
        goto xyz;
    }
    
    while(flag != 0)
    {
        ind = len - 1;
        num = (int)(str.at(ind)) - 48;
        num++;
        
        if(num == 10)
        {
            str.at(ind) = '0';
            ind = ind-1;
            num = (int)(str.at(ind)) -48;
            num++;
        }
        
        ch2 == (char)(num)+48;
        //Reverse order: harder to read.
        /*str.at(ind)=ch2;*/
        ch2 = str.at(ind);
        
        for(std::string::reverse_iterator r = str.rbegin(); r < str.rend(); r++)
        {
            ch = (char)(*r);
            st = st + ch;
        }
        
        if(st == str)
        {
            std::cout << st;
            flag = 0;
        }
    }
    
    xyz: ;
}


Look what a beautiful non-functional code you have here. So let's find its logic.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>


int main()
{
    //The flag and count (logical)
    int flag = 1, count = 0;

    //String and palindrome string
    std::string st;
    std::string str;

    //Ask for input
    std::cout << "Enter a number: ";
    std::cin >> str;

    //Helpful variable for getting the string length
    int len = str.length();
    //By the way, you can use it const only

    //Check if all indexes are 9 (why the hell?)
    for(int j = len - 1; j >= 0; j--)
    {
        if(str.at(j) != '9')
        {
            count = 1;
            break;
        }
    }

    //iff (if and only if) str != all digits 9
    if(count == 0)
    {
        for(int j = 0; j <= len - 2; j++)
        {
            str.at(j) = '0'; //All indexes except the last one are 0
        } 

        str.at(len - 1) = '1'; //The last one is 1
        str = "1" + str; //1 + string (e.g.: "1" + "001" = "1001")

        std::cout << str;

        goto xyz;
    }

    //While not reverse
    //By the way, you can put a do-while to get a better performance; if all digits are 9 you can jump this one.
    while(flag != 0)
    {
        //ind = last index
        //By the way, you can put it after the declaration of len for better performance
        ind = len - 1;
        
        //Number = '1' - '0' = 1 WTH???
        num = (int)(str.at(ind)) - 48;
        
        num++; //Number = 2 WTH???
        
        //More creepy stuff down there. So you can see your code is totally wrong. :)

        if(num == 10)
        {
            str.at(ind) = '0';
            ind = ind-1;
            num = (int)(str.at(ind)) -48;
            num++;
        }

        ch2 == (char)(num)+48;
        ch2 = str.at(ind);

        for(string::reverse_iterator r = str.rbegin(); r<str.rend(); r++)
        {
            ch = (char)(*r);
            st = st+ch;
        }

        if(st == str)
        {
            std::cout << st;
            flag = 0;
        }
    }

    xyz: ;
}


For the last time: what the hell?

So I thought in a way to do it. The easiest one is brute force. After this one, I'll submit the math one.

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
42
43
#include <iostream>
#include <string>
#include <sstream>

std::string to_string(long long num)
{
    std::stringstream ret;
    ret << num;
    return ret.str();
}

long long stoi(std::string str)
{
     std::stringstream help (str);
     long long ret; //Yes, I know, it's not initialized...
     help >> ret;
     return ret;
}



inline std::string reverse(std::string str)
{
    return std::string(str.rbegin(), str.rend());
}

int main()
{
    std::string str;

    std::cout << "Enter a number: ";
    std::cin >> str;

    if(str.size() < 2) str = "10";
    //Even if str = reverse(str), you must get the next palindrome
    do
    {
        str = to_string(stoi(str) + 1);
    }
    while( (str != reverse(str)) );

    std::cout << str;
}


iQChange
Last edited on
Tip: "if(count == 0)" block is fu**ing all your code :)
The thing ia that the numbers can have 100000 digits. How can long long store a number of that size??
Huh... well, it can't :|. GMP is a good shot
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int len,flag=1,count=0,num,ind;
    char ch,ch2;
    string st;
    string str;
    cout<<"Enter a number: ";
    cin>>str;
            len=str.length();
            for(int j=len-1;j>=0;j--)
            {
                if(str.at(j)!='9')
                {
                    count=1;
                    break;
                }
            }
            if(count==0)
            {
                for(int j=0;j<=len-2;j++)
                {
                    str.at(j)='0';
                }
                str.at(len-1)='1';
                str="1"+str;
                cout<<str;
                goto xyz;
            }
            while(flag!=0)
            {
                ind=len-1;
                num=(int)(str.at(ind))-48;
                num++;
                if(num==10)
                {
                    str.at(ind)='0';
                    ind=ind-1;
                    num=(int)(str.at(ind))-48;
                    num++;
                }
                ch2==(char)(num)+48;
                str.at(ind)=ch2;
                for(string::reverse_iterator r=str.rbegin();r<str.rend();r++)
                {
                    ch=(char)(*r);
                    st=st+ch;
                }
                if(st==str)
                {
                cout<<st;
                flag=0;
                }
            }
            cout<<"Hello";
            xyz: ;
}

                    



Look at the code, even "Hello" is not printed if i enter any number whose all digits are not 9. and the no. of digits is larger. FOr eg:- if the input is 5, it gives me the output- "hello". if the input is 2587896, it does not print "hello"/ Why??
Last edited on
Because it stills computing your program.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <string>

static void add(std::string& str, std::size_t index)
{
    if(index > 0)
    {
        if(str[index - 1] < '9')
            str[index - 1] += 1;
        else
        {
            if(!(index - 1))
            {
               str = std::string("1") + std::string(str.size(), '0');
            }
            else
            {
                str[index - 1] = 0;
                return add(str, index - 1);
            }
        }
    }
    else throw("Unexpected error");
}

static void add(std::string& str)
{
    return add(str, str.size());
}

class Number
{
protected:
    std::string number;
public:

    Number() {} // Number num;

    Number(const std::string number_) //Number num("Number")
    {
        //Check for invalid expression
        for(auto ch : number_)
            if(ch - '0' >= 10 || ch - '0' < 0) throw("Invalid expression");
        if(number_.size() < 2) this->number = "10";

        else this->number = number_;
    }

    const std::string str() const { return number; } //Number.str()

    const void operator++(int) //Number++ (number += 1)
    {
        ::add(this->number);
    }

    Number& operator=(const std::string str) // Number = "number"
    {
        *this = Number(str);
        return *this;
    }

    friend std::ostream& operator<<(std::ostream& stream, Number num) //std::cout << Number
    {
        stream << num.str();
        return stream;
    }

    friend std::istream& operator>>(std::istream& stream, Number& num) //std::cin >> Number
    {
        std::string str;
        stream >> str;
        num = str;
        return stream;
    }

    operator std::string() { return this->number;} // Cast operator

};

inline std::string reverse(std::string str)
{
    return std::string(str.rbegin(), str.rend());
}

int main(int argc, char* argv[])
{
    try
    {
        //Get input
        Number num;
        std::cout << "Enter a number: ";
        std::cin >> num;
        do
        {
            num++;
        }
        while((std::string)num != reverse(num));
        std::cout << num;
    }
    catch(const char* exc)
    {
        std::cout << "The program threw the following error and needs to stop: " << exc;
        return -1;
    }
}


How much bigger the number be, more time you will need.
Last edited on
Topic archived. No new replies allowed.