Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:52:46+00:00 2026-06-18T06:52:46+00:00

I made this code that is supposed to increment a number till get the

  • 0

I made this code that is supposed to increment a number till get the next palindrome number of of this inputted number.

The program take the number as string ” cause it may be a very big digits number
( 0 < digits < 1000000 ) ” ….

The Code

int main ()
{
string number = "1243";
int position = number.length()-1;
do
{
    if (number[position] == '9')
    {
        //cout << "hereee";
        number[position] = '0';
        int n1 = (int)number[position-1] - '0';
        n1++;
        number[position-1] = n1 + '0';
        nextPalindrome[position-1];
         cout << number <<"hereee2"<< endl; // only to determine if i get in "if"
    }
    else
    {
        int n1 = (int)number[position] - '0';
        n1++;
        number[position] = n1 + '0';
        cout << number <<"hereee1" << endl; // only to determine if i get in "else"
    }
} while (isPalindrome(number) == false);
}

It’s start to take the digit in the current position and increment it and return it as character again

The Problem

cout << number <<"hereee1" << endl;

this line show the number status while runing and it is like that :

12"6 hereee1

12"7 hereee1

12"8 hereee1

12"9 hereee1

12#0 hereee2

12#1 hereee1

while it must to be

1236 hereee1

1237 hereee1

1238 hereee1

1239 hereee1

1240 hereee2

1241 hereee1

i don’t know where is the error .. can any one help

NOTE : “isPalindrome” is a function take a string as parameter and if the original string equal to its reverse, it returns true .. else return false

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:52:47+00:00Added an answer on June 18, 2026 at 6:52 am

    You don’t handle carryover well… This part is invalid:

        int n1 = (int)number[position-1] - '0';
        n1++;
        number[position-1] = n1 + '0';
    

    This just increases the digit on the previous place. And if it happens to be '9' (same as what Mats Petersson tried to suggest), it just overflows… It should however be carried over to the next digit too… This is a recursive solution to this (beware, there might be syntax errors, I haven’t coded in C++ since ages…):

    /*
    * This function adds one to the specified digit of a 
    * string containing a decimal integer.
    *
    * Contains no checks whatsoever. Behavior is undefined when 
    * not supplied a valid input string.
    */
    int addOneToDigit (string number, int digit)
    {
        if (number[digit] == '9')
        {
            number[digit] = '0';
            //we need to handle getting a longer string too...
            if(digit>0) 
            {
                return addOneToDigit(number, digit-1);
            }
            else
            {
                return "1" + number;
            }
        }
        else
        {
            int n1 = (int)number[digit] - '0';
            n1++;
            number[digit] = n1 + '0';
        }
        return number;
    }
    

    and the main() would look like something like this:

    int main ()
    {
        string number = "1243";
        do
        {
          number = addOneToDigit(number,number.length()-1)
        } 
        while (isPalindrome(number) == false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this code that its supposed to parse a given string separated by
The following code is supposed to take a string that may or may not
I made this code that is supposed to show me an image with text
I found this piece of Java code at Wikipedia that is supposed to shuffle
I've made this html box, that is supposed to have a title and some
I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
I have made this code to open a database(created in SQLite browser) stored in
How can I improve this code? What has made this long winded is the
I am pretty new at C# and .NET, but I've made this code to
I have a problem, and I made this test code to show you my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.