CREATE OWN LIBRARY

Average of Even Numbers

Back to Programming

Description

The program is written to find the average of the even numbers from ‘n’ given inputs. The ‘n’ inputs are taken from the user.

The numbers are first checked, the even numbers are added together and the number of even numbers is counted. Then the sum of the even numbers is divided by the counted value to get the average.

For example, let the given inputs be 1, 2, 3, 4, 5.

Therefore, the average will be: 

( 2 + 4 ) / 2 = 6 / 2 = 3

Algorithm

INPUT: The ‘n’ numbers

OUTPUT: Average of the even numbers.

PROCESS:

Step 1: [Taking the input]

   Read n [number of elements]

   For i=0 to n-1 repeat

                              Read arr[i]

               [End of ‘for’ loop]

Step 2: [Finding the average of the even numbers]

               Set sum<-0

               Set c<-0

               For i=0 to n-1 repeat

                              If arr[i] mod 2 = 0 then

                                             Set sum<-sum+arr[i]

                                             Set c<-c+1

               [End of ‘for’ loop]

               [Finding the average]

               Set avg<-(sum/c)

               Print "The average of the numbers: avg"

Step 3: Stop.

Code

TIME COMPLEXITY:

for(i=0;i<n;i++)----------------------------------O(n)

               {

                              if(arr[i]%2==0)-----------------------O(1)

                              {

                                             sum=sum+arr[i];

                                             c++;

                              }

               }

               //finding the average

               avg=(sum/c);----------------------------------O(1)

The time complexity of finding the average of numbers is O(n) where ‘n’ is the number of elements whose average is to be found.

SPACE COMPLEXITY: 

The space complexity of finding the average of numbers is O(n) where ‘n’ is the number of elements whose average is to be found.