Posted by : Unknown
Thursday, 18 June 2015
import java.util.Scanner;
class ComputeAverage
class ComputeAverage
{
public static void main(String[] args)
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
int inputNumber, sum=0, count=0;
double average;
System.out.print("Enter your first positive integer: ");
inputNumber = input.nextInt();
while (inputNumber != 0)
Scanner input = new Scanner( System.in );
int inputNumber, sum=0, count=0;
double average;
System.out.print("Enter your first positive integer: ");
inputNumber = input.nextInt();
while (inputNumber != 0)
{
sum += inputNumber;
count++;
System.out.print("Enter your next positive integer, or 0 to Stop: ");
inputNumber = input.nextInt();
}
if (count == 0)
sum += inputNumber;
count++;
System.out.print("Enter your next positive integer, or 0 to Stop: ");
inputNumber = input.nextInt();
}
if (count == 0)
{
System.out.println("You didn't enter any data!");
}
else
System.out.println("You didn't enter any data!");
}
else
{
average = ((double)sum) / count;
System.out.println();
System.out.println("You entered " + count + " positive integers.");
System.out.printf("Their average is %1.3f.\n", average);
}
}
}
average = ((double)sum) / count;
System.out.println();
System.out.println("You entered " + count + " positive integers.");
System.out.printf("Their average is %1.3f.\n", average);
}
}
}
Output:
Enter your first positive integer: 12
Enter your next positive integer, or 0 to Stop: 45
Enter your next positive integer, or 0 to Stop: 87
Enter your next positive integer, or 0 to Stop: 0
You entered 3 positive integers.
Their average is 48.000.
Enter your next positive integer, or 0 to Stop: 45
Enter your next positive integer, or 0 to Stop: 87
Enter your next positive integer, or 0 to Stop: 0
You entered 3 positive integers.
Their average is 48.000.
Related Posts :
- Back to Home »
- Example program »
- Get number of inputs from user and Compute Average