Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Soft_Uni_Arrays {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter array of numbers: ");
- // create Array stream -> read the user's input -> solit it -> map each element of the splitted numberes ->
- // parse each element and send it the the given Array
- int[] arrayOfParsedNumbers = Arrays.stream(sc.nextLine().split(" ")).mapToInt(e -> Integer.parseInt(e)).toArray();
- int sumOfEvenNumbers = 0;
- int sumOfOddNumbers = 0; // create variables to store the sums
- //perambulate the elements of the Array with for cycle
- for (int i = 0; i < arrayOfParsedNumbers.length; i++) {
- if (arrayOfParsedNumbers[i] % 2 == 0) {
- sumOfEvenNumbers += arrayOfParsedNumbers[i];
- } else {
- sumOfOddNumbers += arrayOfParsedNumbers[i];
- }
- }
- System.out.printf("The sum of the even numbers is: %d%n ", sumOfEvenNumbers);
- System.out.printf("The sum of the odd numbers is: %d ", sumOfOddNumbers);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement