# SumAndAverage Program Help

• October 4th, 2009, 04:31 PM
Raiderofrice
SumAndAverage Program Help
-----
Almost have it figured out... However, every time the program loops, I think the sum and average get reset to sum=0 and average=1, and I cannot figure out a way around it. Any help would be greatly appreciated!

Write a program that reads an unspecified number of integers from the keyboard. When the user enters a ‘0’ this signifies the end of data entry and doesn’t itself count as one of the entered values. The program must compute the sum of all the numbers and the average value of the numbers. The program must be named “SumAndAverage”.

Code :

```public class SumAndAverage { public static void main(String [] args) {   int number; int sum=0; int average=1; int count = 1;   System.out.print("Enter number: "); number = Keyboard.readInt();   while (number != 0) { System.out.print("Enter number: "); number = Keyboard.readInt(); sum = sum number; average = sum / count; count +; } count = count -1; System.out.println("The sum of these " count " numbers is: " sum); System.out.println("The average of these " count " numbers is: " average);   } }```
• October 4th, 2009, 07:08 PM
helloworld922
Re: SumAndAverage Program Help
The average at the start shouldn't be 1, it should be 0. Also, count should start at 0. There are a few syntax errors inside your while loop, they're probably typo's when you were posting. The real problem could be because of round-off error. In integer math, 3/5 = 0, not 0.6. It's also better to only compute the average once the total sum has been calculated.

Code :

```public class SumAndAverage { public static void main(String[] args) { double average = 0; double sum = 0; int count = 0; while(true) { System.out.print("Enter number: "); int number = new Scanner(System.in).nextInt(); if (number == 0) { break; } sum += number; count++; } average = sum / count; System.out.println(The sum of these " + count + " numbers is: " + sum); System.out.println("The average of these " + count + " numbers is: " + average); } }```
• October 4th, 2009, 08:43 PM
chronoz13
Re: SumAndAverage Program Help
why is it that the variable average must be initialized into '0' not '1'? ive checked the program (the second one)
and theres nothing logically wrong..

it will change the value of the average in a memory space after executing this part...
Code :

`average = sum / count;`

the value of average wont be '1' nor '0' anymore...

```public class SumAndAverage { public static void main(String [] args) {   // Object declaration int number = 1; int sum = 0; int average = 0; int count = 0;   // Indefinite while loop while (number != 0) { count++; System.out.print("Enter number: "); number = Keyboard.readInt(); sum = sum + number; average = sum / count; }   // Results count = count - 1; average = sum / count; System.out.println("The sum of these " + count + " numbers is: " + sum); System.out.println("The average of these " + count +" numbers is: " + average + " and " + sum%count + " / " + count); } }```