In this tutorial, we will discuss a concept of the C++ code to calculate the average of odd and even numbers in an array.
In this article, we are going to learn how to calculate the Average of odd and even numbers of an array in the C++ programming language
When any integer ends in 0,2,4,6,8 and it can be divided by two with the remainder of zero, it is called as an even number.
Example of even numbers – 34,-64,78,788
When any integer ends in 0,1,3,5,7,9 and it cannot be divided without a remainder, it is called as an odd number.
Example for odd numbers – 33,-69,75,785
Here, We can use a modular operator to find odd or even number in an array
if n%2==0, n is an even number – if the number is even, the remainder is zero.
if n%2==1, n is an odd number – if the number is odd, the remainder is one.
Program 1
This program allows the user to calculate the average of odd and even numbers in the given array using “for loop”.
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[6]={50,45,40,35,30,25}; //1 int i,oddSum=0,evenSum=0,oddCount=0,evenCount=0; //2 double avgOdd, avgEven; //3 for(i=0; i<6; i++){ //4 if(arr[i]%2==0){ evenSum=evenSum+arr[i]; //5 evenCount++; } else{ //6 oddSum=oddSum+arr[i]; oddCount++; } } double avgOdd=oddSum/oddCount; //7 double avgEven=evenSum/evenCount; cout<<"The average of odd numbers are:"<<avgOdd; //8 cout<<"\nThe average of even numbers are:"<<avgEven; getch(); return 0; }
When the above code is executed, it produces the following results
The average of odd numbers are:35 The average of even numbers are:40
Method:
Program 2
This program allows the user to choose any value for array length(Number of array elements) and input values into the array. The program will calculate the Average of odd and even numbers from the list using “for loop”.
#include <iostream> #include <conio.h> using namespace std; int main() { int i,oddSum=0,evenSum=0,oddCount=0,evenCount=0,arr_size; //1 cout<<"Enter the size of the array: "; cin>>arr_size; //2 int arr[arr_size]; //3 cout<<"Enter the Array elements: "; //4 for(i=0; i<arr_size; i++){ //5 cin>>arr[i]; } for(i=0; i<arr_size; i++){ //6 if(arr[i]%2==0){ //7 evenSum=evenSum+arr[i]; oddCount++; } else{ oddSum=oddSum+arr[i]; //8 evenCount++; } } double avgOdd=oddSum/oddCount; //9 double avgEven=evenSum/evenCount; cout<<"The average of odd numbers are:"<<avgOdd; //10 cout<<"\nThe average of even numbers are:"<<avgEven; getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array: 6 Enter the array elements: 30 29 28 27 26 25 The average of odd numbers are: 27 The average of even numbers are: 28
Method:
Program 1
This program allows the user to calculate the average of odd and even numbers in the given array using “while loop”.
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[6]={60,65,70,75,80,85}; //1 int i,oddSum=0,evenSum=0,oddCount=0,evenCount=0; /2 double avgOdd,avgEven; //3 i=0; while(i<6){ //4 if(arr[i]%2==0){ //5 evenSum=evenSum+arr[i]; evenCount++; } else{ //6 oddSum=oddSum+arr[i]; oddCount++; } i++; } double avgOdd=oddSum/oddCount; //7 double avgEven=evenSum/evenCount; cout<<"The average of odd numbers are:"<<avgOdd; //8 cout<<"\nThe average of even numbers are:"<<avgEven; getch(); return 0; }
When the above code is executed, it produces the following results
The average of odd numbers are: 75 The average of even numbers are:70
Method:
Program 2
This program allows the user to choose any value for array length(Number of array elements) and input values into the array. The program will calculate the Average of odd and even numbers from the list using “while loop”.
#include <iostream> #include <conio.h> using namespace std; int main() { int i,oddSum=0,evenSum=0,oddCount=0,evenCount=0,arr_size; //1 cout<<"Enter the size of the array: "; //2 cin>>arr_size; int arr[arr_size]; //3 cout<<"Enter the Array elements: ";//4 i=0; while(i<arr_size){ //5 cin>>arr[i]; i++; } i=0; while(i<arr_size){ //6 if(arr[i]%2==0){ //7 evenSum=evenSum+arr[i]; oddCount++; } else{ //8 oddSum=oddSum+arr[i]; evenCount++; } i++; } double avgOdd=oddSum/oddCount; //9 double avgEven=evenSum/evenCount; cout<<"The average of odd numbers are:"<<avgOdd; //10 cout<<"\nThe average of even numbers are:"<<avgEven; getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array: 6 Enter the array elements: 100 99 98 97 96 95 The average of odd numbers are:97 The average of even numbers are:98
Method:
Similar post
Cpp program to check whether a number is even or odd
Cpp program to check a number is even or odd using function
Cpp program to separate even and odd number from an array
Cpp program to display even and odd numbers from 1 to n
Cpp program find greatest of three numbers using function
Display odd and even numbers without if statements
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…