Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I increase an integer by repeating the last digit?

So I need to be able to increase an integer if it is less than 6 digits. If it’s less than six digits, the integer should increase by repeating the last digit until it hits six digits. For example, 1234 becomes 123444. The hint is to use a loop and math equation to increase it 1 digit at a time. I figured out how to decrease the number by one integer by dividing it by 10, but I cannot figure out an equation that would let me increase the number. Here is my code:

 public void setAccountNum(int accountNum) {
        final int MAX_LOAN_NUMBER = 999999;
        if (accountNum > MAX_LOAN_NUMBER) {
            System.out.println("Too many digits in account number " + accountNum);
            while (accountNum > MAX_LOAN_NUMBER) {
                accountNum = accountNum / 10;
            }
            this.accountNum = accountNum;
            System.out.println("   Set to the 6-digit value of " + accountNum + ".");
        }
        if (accountNum < MINIMUM_LOAN_NUMBER) {
            
        }
        else {
            this.accountNum = accountNum;
        }
        
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Let’s take it easy.

// [...]

using namespace std;

int main(void) {
  int n;
  scanf("%d", &n); // Suppose n < 1*10^7
  while (n < 100000) {
    n = n * 10 + n % 10;
  }
  printf("%d\n", n);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading