Q:

Calculate Average of \'N\' Numbers Using Java Program

0

This Program is used to calculate Average of N Numbers using Java Program

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/* AVERAGE CALCULATION OF 'N' NUMBERS */ 

import java.io.*; 
import java.text.*; 

class ssva17 
{ 
public static void main(String arg[]) throws IOException 
{ 
int i,n; 
double a[]=new double[20]; 
double tot=0,avg; 
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println(" AVERAGE CALCULATION "); 
System.out.println(" ------- ----------- "); 
System.out.print("\nENTER HOW MANY NUMBERS : "); 
n=Integer.parseInt(obj.readLine()); 
NumberFormat nf=NumberFormat.getInstance(); 
nf.setMaximumFractionDigits(2); 
nf.setMinimumFractionDigits(2); 
System.out.println("\nENTER THE NUMBERS\n"); 
for(i=1;i<=n;i++) 
{ 
a[i]=Double.parseDouble(obj.readLine()); 
tot+=a[i]; 
} 
System.out.println("\nTOTAL IS : "+nf.format(tot)); 
avg=tot/n; 
System.out.println("\nTHE AVERAGE IS : "+nf.format(avg)); 
System.out.println("\n"); 
} 
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now