Saturday, February 23, 2013

Sentinel

import java.util.Scanner;
public class sentinel {
       
   
        static Scanner input = new Scanner(System.in);
        static final int SENTINEL = -999;
    public static void main (String args[]){
       
       
       
        int number;
        int sum = 0;
        int count = 0;
       
        System.out.println("enter positivel integers ending with " +sum);
        number = input.nextInt();
       
        while (number != SENTINEL){
           
            sum = sum + number;
            count++;
            number = input.nextInt();
        }
       
        System.out.printf("the sum of %d numbers = %d%n",count, sum);
       
       
        if(count != 0)
            System.out.printf("the average = %d%n",(sum/count));
       
        else
            System.out.println("no input" );
       
    }

}

No comments:

Post a Comment