Write a Java Program to find Sum of Odd Numbers in an Array using For Loop, While Loop, and Functions with example.
Java Program to find Sum of Odd Numbers in an Array using For Loop
This program allows the user to enter the size, and the Array elements. Next, it will find the sum of odd numbers within this array using For Loop.
// Java Program to find Sum of Odd Numbers in an Array using For Loop import java.util.Scanner; public class SumOfOdds1 { private static Scanner sc; public static void main(String[] args) { int Size, i, OddSum = 0; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements in an array : "); Size = sc.nextInt(); int [] a = new int[Size]; System.out.print(" Please Enter " + Size + " elements of an Array : "); for (i = 0; i < Size; i++) { a[i] = sc.nextInt(); } for(i = 0; i < Size; i++) { if(a[i] % 2 != 0) { OddSum = OddSum + a[i]; } } System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum); } }
OUTPUT:
ANALYSIS
First, We used for loop to iterate each and every element. Within the Loop we used If statement to check if the number divisible by 2 is not equal to 0 or not
for(i = 0; i < Size; i++) { if(a[i] % 2 != 0) { OddSum = OddSum + a[i]; } }
User inserted values are a[5] = {10, 25, 35, 60, 77}}, OddSum = 0
First Iteration: for (i = 0; 0 < 5; 0++)
The value of i will be 0 and the condition (i < 5) is True. So, it will enter into the If statements inside the loop.
if(a[i] % 2 != 0) => if(a[0] % 2 != 0)
if(10 % 2 != 0) – Condition is False.
Second Iteration: for (i = 1; 1 < 5; 1++)
The condition (i < 5) is True.
if(a[1] % 2 != 0)
if(25 % 2 != 0) – Condition is True.
OddSum = OddSum + a[i]
OddSum = 0 + 25 = 25
Third Iteration: for (i = 2; 2 < 5; 2++)
The condition (i < 5) is True.
if(a[2] % 2 != 0)
if(35 % 2 != 0) – Condition is True.
OddSum = 25 + 35 = 60
Do the same for remaining iterations until the condition (i < 5) fails.
Java Program to find Sum of Odd Numbers in an Array using While Loop
This program is same as above but this time we used While Loop to calculate the sum of odd numbers in an array.
// Java Program to find Sum of Odd Numbers in an Array using While Loop import java.util.Scanner; public class SumOfOdds2 { private static Scanner sc; public static void main(String[] args) { int Size, i = 0, j = 0, OddSum = 0; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements in an array : "); Size = sc.nextInt(); int [] a = new int[Size]; System.out.print(" Please Enter " + Size + " elements of an Array : "); while(i < Size) { a[i] = sc.nextInt(); i++; } while(j < Size) { if(a[j] % 2 != 0) { OddSum = OddSum + a[j]; } j++; } System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum); } }
OUTPUT
Program to find Sum of Odd Numbers in an Array using Methods
This program is same as first example, but this time we separated the logic to calculate the sum of odd numbers using Method.
// Java Program to find Sum of Even Numbers in an Array using Functions import java.util.Scanner; public class SumOfOdds3 { private static Scanner sc; public static void main(String[] args) { int Size, i, OddSum = 0; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements in an array : "); Size = sc.nextInt(); int [] a = new int[Size]; System.out.print(" Please Enter " + Size + " elements of an Array : "); for (i = 0; i < Size; i++) { a[i] = sc.nextInt(); } OddSum = SumOfOddsinArray(a, Size); System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum); } public static int SumOfOddsinArray(int[] a, int Size) { int i, OddSum = 0; for(i = 0; i < Size; i++) { if(a[i] % 2 != 0) { OddSum = OddSum + a[i]; } } return OddSum; } }
OUTPUT