Java Programming | Average of numbers

Introduction

This is tutorial for average of numbers in java. The Program is given below that calculates the average of numbers entered by user and prints it. The program is extendable. Go enjoy the program. Lets begin……..

//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class AvgNo {
	//void main
		public static void main (String[] args)
		{
			//declare int
			int avg,x,y,z;
			//print message
			System.out.println("Enter Three numbers:");
			//Take input
			Scanner input = new Scanner(System.in);
			//calculate
			 x = input.nextInt();
			 y = input.nextInt();
		     z = input.nextInt();
		     avg = (x + y + z)/3;
			//print the average
			System.out.println("Average = "+avg);
		}
}

Output

Enter Three numbers:
1
2
3
Average = 2

How it works

  1. The program prints the message to enter numbers.
  2. The user enters three numbers.
  3. Average is calculated.
  4. The average is printed.

Extending it

The program can be extended by taking more different shapes and calculating their area.

Explanation.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function with the message to enter three numbers.
  5. Declare input as Scanner.
  6. Take the numbers and save it in variable.
  7. Calculate average and save it in other variable.
  8. Add system.out.println() function to print the average.

At the end.

You learnt creating the Java program for Average of numbers. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

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