Advertisement
Kancho

Newest_Sums_Of_Array

Mar 6th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Soft_Uni_Arrays {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. System.out.print("Enter array of numbers: ");
  8. // create Array stream -> read the user's input -> solit it -> map each element of the splitted numberes ->
  9. // parse each element and send it the the given Array
  10. int[] arrayOfParsedNumbers = Arrays.stream(sc.nextLine().split(" ")).mapToInt(e -> Integer.parseInt(e)).toArray();
  11. int sumOfEvenNumbers = 0;
  12. int sumOfOddNumbers = 0; // create variables to store the sums
  13.  
  14. //perambulate the elements of the Array with for cycle
  15. for (int i = 0; i < arrayOfParsedNumbers.length; i++) {
  16. if (arrayOfParsedNumbers[i] % 2 == 0) {
  17. sumOfEvenNumbers += arrayOfParsedNumbers[i];
  18. } else {
  19. sumOfOddNumbers += arrayOfParsedNumbers[i];
  20. }
  21. }
  22. System.out.printf("The sum of the even numbers is: %d%n ", sumOfEvenNumbers);
  23. System.out.printf("The sum of the odd numbers is: %d ", sumOfOddNumbers);
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement