Home > front end >  How can I increase an integer by repeating the last digit?
How can I increase an integer by repeating the last digit?

Time:03-28

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;
        }
        
    }

CodePudding user response:

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);
}
  • Related