Write a program that prompts the user to input a number of positive integer values which is terminated by an appropriate sentinel value (you can choose -1 as the sentinel value) and then prints out the largest value, the smallest value, the average value and the number of prime numbers that exist in the input. For example, if the user input:
3 1 8 9 2 7 -1
the output is:

The largest value = 9
The smallest value = 1
The average value = 5
The number of primes = 3

10 Years
Discussion Span
Last Post by Chaster
import java.util.*;

public class findValue
public static void main (String args[])

int index;
int arrayValue;
int maxIndex;

Scanner read = new Scanner (System.in);

System.out.print("Enter value (-1 to exit): ");
arrayValue[] = read.nextInt();
while (arrayValue != -1);

for (index = 0; index < arrayValue;index++)
arrayValue = read.nextInt( );

for(index = 0; index < arrayValue;index++)
System.out.println(arrayValue + " ");

double largestValue;

for(index = 1; index<arrayValue; index++)
if (arrayValue > largestValue)
largestValue = arrayValue[index];
return index;

System.out.println("The largest value : " + largestValue);


Edited by happygeek: fixed formatting


here is a prime function:

public boolean prime(int x)
for (int i=2;i<x/2;i++) // actually here you can replece x/2 with square root of x
if (x % i == 0) // if you can divide it by a number
return false;   // then it's not prime
return true;   // if you are here you couldn't make any divisin, so the nr is prime

Edited by happygeek: fixed formatting


Wow man. That's piece of cake.

You'll need arrays. Declare it as private int[] array = new int[10];
In order to iterate this you need an integer. the indexing is from 0 (!!!) to that upper limit-1.
You run through the array, and have some variables (min, max, avg). min let be a very big number (e.g maxint), max a very small one (-maxint), avg 0. So when you iterate the array, test whether the actual element is smaller then min, or bigger then max. (Oh and also you write avg += array -> i is the iterator). Whe you reach the last element, print out max, min, avg/nr. of elements


ok thanks alot ... hehe.. i try again.. if still fail.. i tell u again.. ok? thanks u much..


>You'll need arrays.
Wrong, arrays here simply adds unnecessarily to the space complexity problem. They ain't needed to find the largest, smallest and average number.


Right, my bad. I thought we had to make some stats after typing -1 (like: prime numbers were: 2,3,7). :P

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.