Java Program to find Average of N Numbers

You are Here:

Using for loop

In the following example, we will find the average of numbers between 1 and 10 using for loop.

Example

Java Compiler
public class myClass { public static void main(String[] args) { int start = 1; int end = 10; int total = 0; int count = (end - start) + 1; float average = 0; System.out.format("Average of numbers between %d and %d:\n\n", start, end); for(start=start; start<=end; start++) total += start; System.out.println("Total = " +total); System.out.println("Count = " +count); average = (float) total / count; System.out.println("Average is " +average); } }

Output

Average of numbers between 1 and 10: Total = 55 Count = 10 Average is 5.5

Using while loop

In the following example, we will find the average of numbers between 1 and 10 using while loop.

Example

Java Compiler
import java.lang.Math; public class myClass { public static void main(String[] args) { int start = 1; int end = 10; int total = 0; int count = (end - start) + 1; float average = 0; System.out.format("Average of numbers between %d and %d:\n\n", start, end); while(start <= end) { total += start; start++; } System.out.println("Total = " +total); System.out.println("Count = " +count); average = (float) total / count; System.out.println("Average is " +average); } }

Output

Average of numbers between 1 and 10: Total = 55 Count = 10 Average is 5.5

Using do while loop

In the following example, we will find the average of numbers between 1 and 10 using do while loop.

Example

Java Compiler
import java.lang.Math; public class myClass { public static void main(String[] args) { int start = 1; int end = 10; int total = 0; int count = (end - start) + 1; float average = 0; System.out.format("Average of numbers between %d and %d:\n\n", start, end); do { total += start; start++; }while(start <= end); System.out.println("Total = " +total); System.out.println("Count = " +count); average = (float) total / count; System.out.println("Average is " +average); } }

Output

Average of numbers between 1 and 10: Total = 55 Count = 10 Average is 5.5

Find Average of N Numbers for any Given Range

In the following example, we will find the average of N numbers between the user given range.

Example

Java Compiler
import java.util.Scanner; public class myClass { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter a (int) starting number: "); int start = reader.nextInt(); System.out.print("Enter an (int) ending number: "); int end = reader.nextInt(); int total = 0; int count = (end - start) + 1; float average = 0; System.out.format("Average of numbers between %d and %d:\n\n", start, end); for(start=start; start<=end; start++) total += start; System.out.println("Total = " +total); System.out.println("Count = " +count); average = (float) total / count; System.out.println("Average is " +average); } }

Output

Enter a (int) starting number: 2 Enter an (int) ending number: 15 Average of numbers between 2 and 15: Total = 119 Count = 14 Average is 8.5

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author