Calculations

Java code to Calculate average of odd and even in an array

Java code to Calculate average of odd and even in an array

In this tutorial, we will discuss the simple concept of the  Java 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 in the Java programming language

 

Java code to Calculate average

What is an even or odd number?

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  – (it can be negative and positive)

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.

 

Java code to Calculate the average of odd and even numbers using for loop

Program 1

This program allows the user to calculate the average of odd and even numbers in the given array using “for loop”.

 

public class OddEvenAvgArray2{
public static void main(String args[]){
int arr[]={14,43,59,68,71,86};            //1
int oddSum=0,evenSum=0,evenCount=0,oddCount=0;  //2
 
for(int i=0; i<6; i++){    //3
if(arr[i]%2==0){            //4
evenSum=evenSum+arr[i];
evenCount++;
}
else{                    //5
oddSum=oddSum+arr[i];
oddCount++;
}
}
double avgOdd=oddSum/oddCount;  //6
double avgEven=evenSum/evenCount;
System.out.println("\nAverage of even numbers are: "+avgEven);   //7
System.out.println("\nAverage of odd numbers are: "+avgOdd);
}

}

When the above code is executed, it produces the following results

Average of even numbers are:56.0
Average of odd numbers are:57.0

 

Method:

  1. First, define an array with elements.
  2. Next, declare and initialize four variables to find sum as oddSum=0, evenSum=0,oddCount=0,evenCount=0;
  3. Then, use the “for loop” to take the elements one by one from the given array.
  4. The “if statement” finds a number and then if the number is even, it is added to evenSum. If the number is not even, it is dealt with by  “else statement”.
  5. The “else statement”  finds odd numbers and adds to oddSum.
  6. Then, find the average of odd and even numbers
  7. Finally, the average of odd numbers and even numbers are displayed.

 

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”.

import java.util.Scanner;
class OddEvenAvgArray3{
public static void main (String args[]){

int size,oddSum=0,evenSum=0,oddCount=0,evenCount=0;    //1
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the numbers of elements: ");
size=scan.nextInt();                                  //2

int arr[]=new int[size];                      //3
System.out.print("Enter the numbers : ");    //4
for(int i=0; i<size; i++){                     //5
arr[i]=scan.nextInt();
}
System.out.print("Enter numbers: ");
for(int i=0; i<size; i++){           //6
if(arr[i]%2==0){                     //7
evenSum=evenSum+arr[i];
evenCount++;
}
else{                         //8
oddSum=oddSum+arr[i];
oddCount++;
}

}
double avgOdd=oddSum/oddCount;     //9
double avgEven=evenSum/evenCount;
System.out.println("\nAverage of even numbers are: "+avgEven);       //10
System.out.println("\nAverage of odd numbers are: "+avgOdd);
}

}

When the above code is executed, it produces the following results

Enter the number of elements:7
Enter the numbers : 100
101
102
103
104
105
106

Average of even numbers are: 103.00
Average of odd numbers are: 103.00

Method:

  1. To begin with , declares and initializes  variables: oddSum=0, evenSum=0,ddCount=0,evenCount=0 and size.
  2. Next, receives input from the user regarding the array length.
  3. Then, defines an array without including the elements.
  4. Afterwards, the elements for an array is received from the user.
  5. Then, using the “for loop”, the elements are added one by one to the array.
  6. Then, using the second “for loop”, elements are picked one by one from the array to determine oddness and evenness.
  7. Next, “if statements” are used to find a number and then if it is even, it is added to evenSum.If the number is not even, it is dealt with by  “else statement”.
  8. After that, the “else statement” adds to oddSum.
  9. Then, find the average of odd and even numbers
  10. Finally, the average of odd numbers and even numbers are displayed.

 

Java code to Calculate the average of odd and even numbers using while loop

Program 1

This program allows the user to calculate the average of odd and even numbers in the given array using “while loop”.

public class OddEvenAvgArray1{
public static void main(String args[]){
int oddSum=0,evenSum=0,evenCount=0,oddCount=0;      //1
int arr[]={13,44,58,67,70,85};                    //2
int i=0; 
while(i<6){                   //3
if(arr[i]%2==0){           //4
evenSum=evenSum+arr[i];
evenCount++;
}
else{                   //5
oddSum=oddSum+arr[i];
oddCount++;
}
 i++;
}
double avgOdd=oddSum/oddCount;    //6
double avgEven=evenSum/evenCount;
System.out.println("\nSum of even numbers are:"+avgEven);     //7
System.out.println("\nSum of odd numbers are:"+avgOdd);
}
}

 

When the above code is executed, it produces the following results

Average of even numbers are:55.0
Average of odd numbers are:57.0

Method:

  1. First, define an array with elements.
  2. Next, declare and initialize variables to find sum as oddSum=0, evenSum=0 evenCount=0,oddCount=0 and size
  3. Then, use the “while loop” to take the elements one by one from the array.
  4. The “if statement” finds a number and then if the number is even, it is added to evenSum. If the number is not even, it is dealt with by  “else statement”.
  5. The “else statement”  finds odd numbers and adds to oddSum.
  6. Then calculate the average of odd and even numbers
  7. Finally, the average of odd numbers and even numbers are displayed.

 

 

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“.

import java.util.Scanner;
class OddEvenAvgArray4{
public static void main (String args[]){

int size,oddSum=0,evenSum=0,oddCount=0,evenCount=0;     //1
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the numbers of elements: ");
size=scan.nextInt();                                   //2

int arr[]=new int[size];                         //3
System.out.print("Enter the numbers : ");   //4
int i=0; 
while(i<size){                         //5
arr[i]=scan.nextInt();
 i++;
}

i=0;
while(i<size){                       //6
if(arr[i]%2==0){           //7
evenSum=evenSum+arr[i];
 evenCount++;
}
else{                           //8
oddSum=oddSum+arr[i];
oddCount++;
}
i++;
}
double avgOdd=oddSum/oddCount;         //9
double avgEven=evenSum/evenCount;
System.out.println("\nAverage of even is: "+avgOdd);      //10
System.out.println("\nAverage of odd is: "+avgEven);
}
}

 

When the above code is executed, it produces the following results

Enter the number of elements:7
Enter the numbers : 15
16
17
18
19
20

Average of even numbers are: 17.00
Average of odd numbers are: 18.00

 

Method:

  1. To begin with , declares and initializes variables: oddSum=0, evenSum=0, evenCount=0,oddCount=0 and size.
  2. Next, receives input from the user regarding the array length.
  3. Then, defines an array without including the elements.
  4. Afterwards, the elements for an array is received from the user.
  5. Then, using the “while loop”, the elements are added one by one to the array.
  6. Then, using the second “while loop”, elements are picked one by one from the array to determine oddness and evenness.
  7. Next, “if statements” are used to find a number and then if it is even, it is added to evenSum. If the number is not even, it is dealt with by  “else statement”.
  8. After that, the “else statement” find odd numbers and adds to oddSum.
  9. Then, calculate the average value of odd and even numbers
  10. Finally, the average of odd numbers and even numbers are displayed.

 

Similar post

C program to find a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

C program to find whether a number is even or odd

 

C++ program to find a number is even or odd using the function

C++ program to separate Odd and Even numbers from an array

C++ program to display all even and odd numbers from 1 to n

C++ program calculate Average of odd and even numbers in an array 

C++ program to calculate the sum of odd and even numbers

C++ program to find whether a number is even or odd

 

Java program to find a number is even or odd using the method

Java program to separate Odd and Even numbers from an array

Java program to display all even and odd numbers from 1 to n

Java program display odd and even numbers without if statements

Java program to calculate the sum of odd and even numbers

Java program to find whether a number is even or odd

 

Suggested for you

Java method

Operator in Java

If statements in Java

Data  type and variable in Java

 

 

Calculate sum of odd and even numbers in a list in Python
C++ code to Calculate average of odd and even in an array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago