Java classic topic exercises-simple implementation of the eighth question..., java classic
* [Program 8] * question: calculate the value of s = a + aa + aaa + aaaa + aa... a, where a is a number. *
For example, 2 + 22 + 222 + 2222 + 22222 (a total of 5 numbers are added at this time), and the addition of several numbers is controlled by the keyboard.
Thoughts:
The preceding calculation methods can be divided: 5*2*10 ^ 0 + 4*2*10 ^ 1 + 3*2*10 ^ 2 + 2*2*10 ^ 3 + 1*2*10 ^ 4
The corresponding program implementation is as follows:
Public class Test08 {
Public static void main (String [] args ){
Repeated scan = new partition (System. in );
System. out. println ("enter a number :");
Int a = scan. nextInt ();
System. out. println ("Enter the number of computations ");
Int num = scan. nextInt ();
Int sum = 0;
For (int I = 1; I <= num; I ++ ){
Sum + = (num-I + 1) * a * (Math. pow (10, I-1 ));
For (int j = 1; j <= I; j ++ ){
System. out. print ();
}
If (I! = Num ){
System. out. print ("+ ");
}
}
System. out. println ("=" + sum );
}
}