import java.util.Scanner;
import java.text.DecimalFormat;
public class average_of_N
{
public void format(double d)
{
System.out.println(d);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.print("Enter the quantity of numbers for averaging : ");
int n = sc.nextInt();
int temp = 0;
for(int i=0;i<n;i++)
{
System.out.print("Enter the number for averaging : ");
int x = sc.nextInt();
int sum = temp + x;
temp = sum;
}
double ave = temp / n;
System.out.println("Average =" + df.format(ave));
}
}
Output :
Enter the quantity of numbers for averaging : 2
Enter the number for averaging : 3
Enter the number for averaging : 4
Average =3.00