Java classic topic exercises-simple implementation of the eighth question..., java classic

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.