Advertisement
Kancho

Sum_Of_Even_And_Odd_Nunbers_Of_Array

Mar 5th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Soft_Uni_Arrays {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. System.out.print("Enter array of numbers: ");
  7.  
  8. String[] numbersAsString = sc.nextLine().split(" "); //split the user's input and save it in array numbersAsString
  9. int[] arrayOfParsedNumbers = new int[numbersAsString.length]; // create a new array arrayOfParsedOfParsedNumbers
  10.  
  11. for (int i = 0; i < numbersAsString.length; i++) { // perambulate the array numbersAsString with for cycle
  12. arrayOfParsedNumbers[i] = Integer.parseInt(numbersAsString[i]); // abstract the numbers from the array numbersAsStrings and parse them
  13. }
  14. int sumOfEvenNumbers = 0; // create variables sum to save the sums
  15. int sumOfOddNumbers = 0;
  16.  
  17. for (int i = 0; i < arrayOfParsedNumbers.length; i++) { // perambulate the array arrayOfParsedNumbers with for cycle
  18. if (arrayOfParsedNumbers[i] % 2 == 0) { // if check - if the number of the array is even or odd
  19. sumOfEvenNumbers += arrayOfParsedNumbers[i]; //accumulate the even numbers
  20. } else {
  21. sumOfOddNumbers += arrayOfParsedNumbers[i]; // accumulate the odd numbers
  22. }
  23. }
  24. System.out.printf("The sum of even numbers is %d%n", sumOfEvenNumbers);
  25. System.out.printf("The sum of odd numbers is %d", sumOfOddNumbers);
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement