Answers for "additon of array odd numbers java"

1

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

// Java program to calculate sum of even and odd numbers in an array
import java.util.Scanner;
public class FindSumOf
{
   public static void main(String[] args)
   {
      int number, sumEven = 0, sumOdd = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements in an array: ");
      number = sc.nextInt();
      int[] arrNumbers = new int[number];
      System.out.println("Please enter elements of the array: ");
      for(int a = 0; a < number; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      for(int a = 0; a < number; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            sumEven = sumEven + arrNumbers[a];
         }
         else
         {
            sumOdd = sumOdd + arrNumbers[a];
         }
      }
      System.out.println("Sum of even numbers in an array: " + sumEven);
      System.out.println("Sum of odd numbers in an array : " + sumOdd);
      sc.close();
   }
}
Posted by: Guest on February-22-2021
3

sum numbers in array java

public class Exercise2 {
public static void main(String[] args) {      
int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;

for (int i : my_array)
    sum += i;
System.out.println("The sum is " + sum);
}
}
Posted by: Guest on October-29-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language