Problem statement:- Print Average of Numbers in array at Odd position.
Data requirement:-
Input Data:- arr,size
Output Data:-avg
Output Data:-avg
Additional Data:- i, j, cout, sum
Program in C
Here is the source code of the C Program to Print Average of Numbers in array at Odd 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,;
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 odd position is %lf",avg);
}
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],j=0,sum=0,cout=0,;
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 odd position is %lf",avg);
}
Input/Output:
Enter the size of the array:5
Enter the Element of the array:
7
9
3
4
2
Average of Numbers in array at odd position is 4.000000
Enter the Element of the array:
7
9
3
4
2
Average of Numbers in array at odd position is 4.000000
Program in C++
Here is the source code of the C++ Program to Print Average of Numbers in array at Odd 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 odd position is "<<avg;
}
Input/Output: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 odd position is "<<avg;
}
Enter the size of the array:6
Enter the Element of the array:
4
2
7
9
5
3
Average of Numbers in array at odd position is 5.33333
Enter the Element of the array:
4
2
7
9
5
3
Average of Numbers in array at odd position is 5.33333
Program in Java
Here is the source code of the Java Program to Print Average of Numbers in array at Odd position.
Code:
import java.util.Scanner;
public class p12 {
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 Odd position is "+avg);
sc.close();
}
}
public class p12 {
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 Odd position is "+avg);
sc.close();
}
}
Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
3
7
9
5
Average of Numbers in array at Odd position is 6.0
4
Enter the Element of the array:
3
7
9
5
Average of Numbers in array at Odd position is 6.0
Program in Python
Here is the source code of the Python Program to Print Average of Numbers in array at Odd 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(0, size):
if ((j+1) % 2 == 1):
sum += arr[j]
cout+=1
avg = (sum / cout)
print("Average of Numbers in array at odd position is ", avg)
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(0, size):
if ((j+1) % 2 == 1):
sum += arr[j]
cout+=1
avg = (sum / cout)
print("Average of Numbers in array at odd position is ", avg)
Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
4
7
6
2
3
Average of Numbers in array at odd position is 4.333333333333333
Enter the Element of the array:
4
7
6
2
3
Average of Numbers in array at odd position is 4.333333333333333
Most Recommend Questions:-
More Questions:-
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box