Dear sourcecodester.com…
Dear sourcecodester.com owner, Great post!
Enter data for processing:
12 8 9 2 3 90 38 56 8 23 89 7 2 8 3 8
The sum of the 12 numbers = 335
The average = 27
The preceding program works as follows: the statement System.out.println("Enter the data for processing:");
prompts the user to input the data for processing.
The statement limit = console.nextInt();
reads the next input line and stores it in the variable limit. The value of limit indicates the number of items to be read.
The statements sum = 0;
and counter = 0;
initialize the variables sum and counter to 0.
The while statement while (counter < limit)
checks the value of counter to determine how many have been read. If the counter is less than limit, the while loop proceeds for the next iteration.
The statement number = console.nextInt();
stores the next number in the variable number.
The statement sum = sum + number;
updates the value of sum by adding the value of number to the previous value.
The statement counter++;
increments the value of counter by 1.
The statement System.out.println(" The sum of the number is = " + sum);
outputs the sum of the numbers.
The statement [geshifilter-java if (counter != 0)
System.out.println(" the average is = " + (sum / counter));
else
System.out.println("no input data");