Java 1.5: averaging numbers

average.png

import java.util.Scanner;

public class Main {

    static int [] num = {0,0,0};

    public static void main(String[] args) {

        int sum;
        double average;

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        num[0] = scan.nextInt();
        // reads and stores the first integer using nextInt method

        System.out.print("Enter the second number: ");
        num[1] = scan.nextInt();

        System.out.print("Enter the third number: ");
        num[2] = scan.nextInt();

        sum = num[0] + num[1] + num[2];

        average = sum / 3.0;
        // dividing by 3 would give the nearest whole number

        System.out.println("The sum is " + sum);
        System.out.println("The average is " + average);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s