Design an algorithm which accepts n integer values and calculates the average and prints it.
Input:
First Line:Number of elements in the array
Second Line:Elements of the list
Output: Average of them
import java.io.*; import java.util.*; public class temp{ public static void main(String[] args){ Scanner s=new Scanner(System.in); int sum=0,i; System.out.printf("Enter the n Number: "); int n=s.nextInt(); int[] b=new int[n]; System.out.printf("Enter the numbers: "); for(i=0;i<n;i++) b[i]=s.nextInt(); for(i=0;i<n;i++) sum=sum+b[i]; System.out.printf("Average number is %.2f \n\n",(float)sum/n); } }
OUTPUT:
Enter the n Number: 5
Enter the numbers: 23 4 65 656 66
Average number is 162.80
Enter the n Number: 5
Enter the numbers: 1 2 3 4 5
Average number is 3.00
Enter the n Number: 8
Enter the numbers: 8 16 2 5 4 8 9 66
Average number is 14.75
Enter the n Number: 2
Enter the numbers: 1 1
Average number is 1.00
ScrShot