Java program computing 2 + 22 + 222 +... perfect answer to the question, java222
I encountered a problem in my interview question recently. The requirement is probably 2 + 22 + 222 + 2222 + 22222... such a group of numbers, Baidu finds that all the answers have bugs, that is, they can only calculate single digits, that is, one digit, and more than two digits, the Code is as follows:
Import java. util. collections;
Public class {
Public static void caculate (int a, int m ){
Int I = 1;
Long sum = 0;
Long j = 0;
Int k =;
While (I <= m ){
J = j + k;
K = (int) (k * Math. pow (10, String. valueOf (a). length ()));
Sum = sum + j;
++ I;
}
System. out. println ("the calculated value is:" + sum );
}
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
System. out. println ("Enter the value of :");
Int a = SC. nextInt ();
System. out. println ("Enter m value :");
Int m = SC. nextInt ();
Caculate (a, m );
}
}