Calculate sum of odd and even of an array in Java
- Home
- Calculations
- Calculate sum of odd and even of an array in Java
- On
- By
- 0 Comment
- Categories: Calculations
Calculate sum of odd and even of an array in Java
Calculate sum of odd and even of an array in Java
In this tutorial, we will discuss how to use the Java program to calculate the sum of odd and even numbers in an array.
In this article, we are going to learn how to calculate the sum of odd and even numbers in the Java programming language
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
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.
Calculating the sum of odd and even numbers using “for loop”
Program 1
This program allows the user to calculate the sum of odd and even numbers in the given array using “for loop”.
Program 1
public class OddEvenSumArray{ public static void main(String args[]){ int arr[]={12,43,57,66,69,84}; int oddSum=0,evenSum=0; for(int i=0; i<6; i++){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } } System.out.println("\nSum of even is: "+evenSum); System.out.println("\nSum of odd is: "+oddSum); } }
When the above code is executed, it produces the following results
Sum of even is: 162 Sum of odd is:169
Method:
- First, define an array with elements.
- Next, declare and initialize two variables to find sum as oddSum=0, evenSum=0
- Then, use the “for loop” to take the elements one by one from the array.
- 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”.
- The “else statement” finds odd numbers and adds to oddSum.
- Finally, the sums of odd numbers and even numbers are displayed.
Program 2
This program allows the user to choose any value for array length and input values into the array. The program will calculate the sum of odd and even numbers from the array.
import java.util.Scanner; class OddEvenSumArray2{ public static void main (String args[]){ int size,oddSum=0,evenSum=0; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the numbers of elements: "); size=scan.nextInt(); int arr[]=new int[size]; System.out.print("Enter the numbers : "); for(int i=0; i<size; i++){ arr[i]=scan.nextInt(); } for(int i=0; i<size; i++){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } } System.out.println("\nSum of even is: "+evenSum); System.out.println("\nSum of odd is: "+oddSum); } }
When the above code is executed, it produces the following results
Enter the number of elements:6 Enter the numbers: 23 32 43 54 65 76 Sum of even is: 162 Sum of odd is: 131
Method:
- To begin with , declares and initializes three variables: oddSum=0, evenSum=0 and size.
- Next, receives input from the user regarding the array length.
- Then, defines an array without including the elements.
- Afterwards, the elements for an array is received from the user.
- Then, using the “for loop”, the elements are added one by one to the array.
- Then, using the second “for loop”, elements are picked one by one from the array to determine oddness and evenness.
- 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”.
- After that, the “else statement” adds to oddSum.
- Finally, the sums of odd numbers and even numbers are displayed.
Calculating the sum of odd and even numbers using “while loop”
Program 1
This program allows the user to calculate the sum of odd and even numbers in the given array using “while loop”.
Program 1
public class OddEvenSumArray1{ public static void main(String args[]){ int arr[]={13,44,58,67,70,85}; int oddSum=0,evenSum=0; int i=0; while(i<6){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } i++; } System.out.println("\nSum of even is: "+evenSum); System.out.println("\nSum of odd is: "+oddSum); } }
When the above code is executed, it produces the following results
Sum of even is: 172 Sum of odd is: 165
Method:
- First, define an array with elements.
- Next, declare and initialize two variables to find sum as oddSum=0, evenSum=0
- Then, use the “while loop” to take the elements one by one from the array.
- 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”.
- The “else statement” finds odd numbers and adds to oddSum.
- Finally, the sums of odd numbers and even numbers are displayed.
Program 2
This program allows the user to choose any value for array length and input values into the array. The program will calculate the sum of odd and even numbers from the array.
import java.util.Scanner; class OddEvenSumArray3{ public static void main (String args[]){ int size,oddSum=0,evenSum=0; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the numbers of elements: "); size=scan.nextInt(); int arr[]=new int[size]; System.out.print("Enter the numbers : "); int i=0; while(i<6){ arr[i]=scan.nextInt(); i++; } i=0; while(i<6){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } i++; } System.out.println("\nSum of even is: "+evenSum); System.out.println("\nSum of odd is: "+oddSum); } }
When the above code is executed, it produces the following results
Enter the number of elements:6 Enter the numbers: 13 24 35 46 57 68 Sum of even is: 139 Sum of odd is: 105
Method:
- To begin with , declares and initializes three variables: oddSum=0, evenSum=0 and size.
- Next, receives input from the user regarding the array length.
- Then, defines an array without including the elements.
- Afterwards, the elements for an array is received from the user.
- Then, using the “while loop”, the elements are added one by one to the array.
- Then, using the second “while loop”, elements are picked one by one from the array to determine oddness and evenness.
- 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”.
- After that, the “else statement” adds to oddSum.
- Finally, the sums 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
suggested for you
No Comments
Monika December 14, 2020 at 5:54 pm
helpfull code..clearly explained..!!