Print Average of Numbers in array at even position

Problem statement:- Print Average of Numbers in array at even position.

Data requirement:-

   Input Data:- arr,size

  Output Data:-avg

  Additional Data:- i, j, sum, count

Program in C

Here is the source code of the C Program to Print Average of Numbers in array at even position.

Code:

#include<stdio.h>
int main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size],j=0,sum=0,cout=0,i;
    double avg;

    printf("Enter the Element of the array:\n");
     for(j=1;j<=size;j++)
    {
        scanf("%d",&arr[j]);
        if(j%2==0){
            sum+=arr[j];
            cout++;
        }
    }
    avg=(double)(sum/cout);
    printf("Average of Numbers in array at even position is %lf",avg);
    }

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
7
4
6
9
3
Average of Numbers in array at even position is 6.000000

Program in C++

Here is the source code of the C++ Program to Print Average of Numbers in array at even position.

Code:

#include<iostream>
using namespace std;
int main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size],j=0,sum=0,cou=0,i;
    double avg;

    cout<<"Enter the Element of the array:\n";
     for(j=1;j<=size;j++)
    {
        cin>>arr[j];
        if(j%2==0){
            sum+=arr[j];
            cou++;
        }
    }
    avg=(double)sum/(double)cou;
    cout<<"Average of Numbers in array at even position is "<<avg;
    }

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
5
8
13
11
Average of Numbers in array at even position is 9.5

Program in Java

Here is the source code of the Java Program to Print Average of Numbers in array at even position.

Code:

import java.util.Scanner;
public class p11 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size;
    size=sc.nextInt();
    int arr[ ]=new int[size];
int j,sum=0,cout=0;
double avg;
System.out.println("Enter the Element of the array:");
       for(j=0;j<size;j++)
        {
        arr[j]=sc.nextInt(); 
        if((j+1)%2==0){
            sum+=arr[j];
            cout++;
        }
    }
     avg=(double)sum/(double)cout;
     System.out.println("Average of Numbers in array at even position is "+avg);
     sc.close();
}
}

Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
4
7
6
8
2
Average of Numbers in array at even position is 7.5

Program in Python

Here is the source code of the Python Program to Print Average of Numbers in array at even position.

Code:

arr=[]
cout=0
sum=0
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
for j in range(1, size+1):
    if (j % 2 == 0):
        sum += arr[j]
        cout+=1
avg = (sum / cout)
print("Average of Numbers in array at even position is ", avg)

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
4
7
6
4
5
Average of Numbers in array at even position is  5.5

Post a Comment

0 Comments