Java Arrays | Set 1 | Practice | GeeksforGeeks

For a given array of price of items,you have to calculate the average of all prices upto 2 decimal places.
Note: Sum is printed automatically, you only need to calculate and return the average
 
Example 1:
Input:
5
1 2 3 4 5
Output:
15 3.00 
Explanation:
Sum of the array is 15, hence 
average is 15/5=3.00. 


Example 2:
Input:
9
2 55 85 656 52 554 545 5 2
Output:
1956 217.33 
Explanation:
Sum of the array is 1956, hence 
average is 1956/9= 217.33. 

 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function average() which takes the array A[] and  its size N as inputs and returns the average of all the items as a String.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=100
1<=Pi<=1000
 
 
 
0
sayanmazumder9993 weeks ago
JAVA
      double sum = 0;      for(int i = 0; i<N; i++) sum += A[i];      sum = sum /N;      return String.format("%.2f",sum);
+1
amkamble222 months ago
// Correct Output 

class Compute
{
   String average(int A[], int N)
   {
       int sum =0;
       for (int i=0; i< N ; i++){
           
           sum = sum + A[i];
           
       }
       float average = (float)sum / N;
       
       return String.format("%.2f",average);
       
   }
}
0
abhijeetjha020002 months ago
class Compute{   String average(int A[], int N){       int sum = 0;       for(int i=0;i<N;i++){           sum += A[i];       }       float average = (float)sum/N;       return String.format("%.2f", average);   }}
0
muskang87012 months ago
class Compute{   String average(int A[], int N)   {       float av = 0;       for(int i = 0; i < N; i++) {           av += A[i];       }       return String.format("%.2f", av/N);   }}
-1
kothariaman853 months ago
// { Driver Code Starts
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;class GFG{   public static void main (String[] args) throws NumberFormatException, IOException {    BufferedReader br = new BufferedReader(               new InputStreamReader(System.in));  int t = Integer.parseInt(br.readLine()); String str; String st[];                       while(t>0)           {            int N = Integer.parseInt(br.readLine());            int sum = 0;                        int A[] = new int[N];            str = br.readLine();      st = str.split(" ");            for (int i = 0; i < N; i++)      {       A[i]= Integer.parseInt(st[i]);       sum+=A[i];      }      Compute ob = new Compute();            String s=ob.average(A, N);            System.out.println(sum+" "+s);               t--;           }   }}// } Driver Code Ends
 
class Compute{   String average(int A[], int N)   {       float avg=0;       for(int i=0;i<N;i++){           avg+=A[i];       }       avg=avg/N;       return String.format("%.2f",avg);   }}
0
ratnapriya_4g4 months ago
class Compute{   String average(int A[], int N)   {       double sum=0;       float avg;       for(int i=0;i<N;i++){           sum=sum+A[i];       }       avg=(float)sum/N;       return String.format("%.2f",avg);   }
-1
pankajkumarravi6 months ago
************************* Java Logic *****************
String average(int A[], int N) {        double sum=0;        int i=0;        while (i<N){            sum =sum +A[i];            i++;        }        return String.format("%.2f",sum/N);}
0
harshitjinodiya
This comment was deleted.
0
VIVEK SAXENA7 months ago
VIVEK SAXENA
***********************************java**************************************
int sum = 0;    float avg = 0;        for (int i = 0; i < N; i++){sum+=A[i];}    avg = (float) (sum*1.0/N);    String s=String.format("%.2f",avg);
    return s;
0
Mitranjan kumar11 months ago
Mitranjan kumar
class Compute{    String average(int A[], int N)    {        double sum=0,avg =0;        for(int i=0;i<n;i++){ sum="sum+A[i];" }="" avg="sum/N;" return="" string.format("%.2f",avg);="" }="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.