Average of a stream of numbers | Java program to return Average of a stream of numbers ; Returns the new average after including x ; Prints average of a stream of numbers ; Driver Code
class GFG { static int sum , n ; static float getAvg ( int x ) { sum += x ; return ( ( ( float ) sum ) / ++ n ) ; } static void streamAvg ( float [ ] arr , int n ) { float avg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { avg = getAvg ( ( int ) arr [ i ] ) ; System . out . println ( " Average ▁ of ▁ " + ( i + 1 ) + " ▁ numbers ▁ is ▁ " + avg ) ; } return ; } public static void main ( String [ ] args ) { float [ ] arr = new float [ ] { 10 , 20 , 30 , 40 , 50 , 60 } ; int n = arr . length ; streamAvg ( arr , n ) ; } }