Question: calculate the value of s = a + aa + aaa + aaaa + aa... a, where a is a number ., Aaaaaa...

Source: Internet
Author: User

Question: calculate the value of s = a + aa + aaa + aaaa + aa... a, where a is a number ., Aaaaaa...
I. First Writing Method

 

Package com. pb. demo1; import java. util. answer;/*** question: s = a + aa + aaa + aaaa + aa... the value of a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (a total of 5 numbers are added at this time), * the number of a and the number of accumulated digits is received from the keyboard. Program Analysis: * 1. Receive a number input from the keyboard (numbers can only be between 1 and 9) * 2. Receive a number to indicate the number of accumulated digits * 3. for example, if you enter 4 (the number of accumulated digits), the value of a on the keyboard is 5: s = 5 + 55 + 555 + 5555 4. enter a 5: s = a + aa + aaa + aaaa + aaaaa * First Writing Method */public class Demo2 {public static void main (String [] args) {// declare a Scanner variable named Scanner input = new Scanner (System. in); System. out. println ("Enter the numbers involved in the calculation 1-9:"); int n = input. nextInt (); System. out. println ("Enter the number of operations:"); int num = input. nextInt (); // defines the intermediate variable of each loop to calculate the number of double nums = 0.0 each time; // The total number of last listen double sum = 0.0; for (int I = 0; I <num; I ++) {// Add N to the number of each time to the new n value nums + = n; // sum + = nums; // The value of n is added with n = n * 10 each time; System. out. println ("the number of each operation is:" + nums);} System. out. println ("the final sum is:" + sum );}}

 

Ii. method 2
Package com. pb. demo1; import java. util. answer;/*** question: s = a + aa + aaa + aaaa + aa... the value of a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (a total of 5 numbers are added at this time), * the number of a and the number of accumulated digits is received from the keyboard. Program Analysis: * 1. Receive a number input from the keyboard (numbers can only be between 1 and 9) * 2. Receive a number to indicate the number of accumulated digits * 3. for example, if you enter 4 (the number of accumulated digits), the value of a on the keyboard is 5: s = 5 + 55 + 555 + 5555 4. enter a 5: s = a + aa + aaa + aaaa + aaaaa * method 2 */public class Demo3 {public static void main (String [] args) {// declare a Scanner variable named Scanner input = new Scanner (System. in); System. out. println ("Enter the numbers involved in the calculation 1-9:"); int n = input. nextInt (); System. out. println ("Enter the number of operations:"); int num = input. nextInt (); // defines the intermediate variable of each loop to calculate the number of double nums = 0.0 each time; // The total number of last listen double sum = 0.0; for (int I = 0; I <num; I ++) {// calculate nums + = Math using the mathematical power function. pow (10, I) * n; sum + = nums; System. out. println ("the number of each operation is:" + nums);} System. out. println ("the final sum is:" + sum );}}

 

Related Article

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.