category

Calculate average of odd and even numbers in Java

Calculate the average of odd and even numbers in Java

In this tutorial, we will discuss a concept of the  Java code to calculate the average of odd and even numbers.

In this article, we are going to learn  how to  calculate the Average of odd and even numbers  in the Java programming language

Calculate the average of odd and even numbers

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

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 until a given number

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.

 

Calculate the average of odd and even using for loop

Program 1

This program allows the user to enter a value and calculate average of odd and even numbers until the entered value using “for loop”.

Program

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

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

System.out.print("Enter the value of num: "); //3
int num=scan.nextInt();

for(i=1; i<=num; i++){        //4
    if(i % 2==0){              //5
        evenSum=evenSum+i;
        evenCount++;
    }
    else{                      //6
        oddSum=oddSum+i;
        oddCount++;
    }
}
evenAvg=evenSum/evenCount;    //7
oddAvg=oddSum/oddCount;
System.out.println("Average of odd numbers are: "+oddAvg);   //8
System.out.print("Average of even numbers are: "+evenAvg);
    
}
}

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

Enter the value of num:10
Average of odd numbers are: 5.0
Average of even numbers are: 6.0

 

Method:

  1. To begin with , declare and initialize four variables to find  sum as oddSum=0, evenSum=0, oddCount=0 and evenCount=0;
  2. Declare two variables for find average named as avgOdd, avgEven;
  3. Next, a number is received from the user as an input for variable num
  4. Then, using the “for loop“, elements are picked one by one from the numbers to determine oddness and evenness.
  5. 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“.
  6. After that, the “else statement” adds to oddSum.
  7. Then, find the average of odd and even numbers
  8. Finally, the average of odd numbers and even numbers are displayed.

Calculate the average of odd and even using while loop

program

This program allows the user to enter a value and calculate average of odd and even numbers until the entered value using “while loop”.

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

int i, 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 value of num: ");    //2
int num=scan.nextInt();
i=1; 
while(i<=num){         //3
    if(i % 2==0){          //4
        evenSum=evenSum+i;
        evenCount++;
    }
    else{        //5
        oddSum=oddSum+i;
        oddCount++;
    }
     i++;
}
double evenAvg=evenSum/evenCount;     //6
double oddAvg=oddSum/oddCount;
System.out.println("Average of odd numbers are: "+oddAvg);   //7
System.out.print("Average of even numbers are: "+evenAvg);
    
}
}

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

Enter the value of num:12
Average of odd numbers are: 6.0
Average of even numbers are: 7.0

Method:

  1. To begin with , declare and initialize four variables to find  sum as oddSum=0, evenSum=0, oddCount=0 and evenCount=0;
  2. Next, a number is received from the user as an input for variable num
  3. Then, using the “while loop“, elements are picked one by one from the numbers to determine oddness and evenness.
  4. 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“.
  5. After that, the “else statement” 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.

Similar post

Java code to check whether a number is even or odd

Java code to check  a number is even or odd using method

Java code to display all even and odd number from 1 to n

Java program to separate even and odd number from an array

Java code to display even and odd numbers without if statements

Java program to display even and odd numbers in the given range

Java Program to calculate the sum of odd and even numbers

Java Program to calculate the sum of odd and even numbers in an array

Java code to calculate the average of odd and even numbers in an array

 

suggested for you

For loop in Java

While loop in java

If statement in Java

Single dim array in Java

Calculate average of odd and even numbers in C++
C++ program to print even or odd in given range using recursion
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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

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