Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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: ");
- String[] numbersAsString = sc.nextLine().split(" "); //split the user's input and save it in array numbersAsString
- int[] arrayOfParsedNumbers = new int[numbersAsString.length]; // create a new array arrayOfParsedOfParsedNumbers
- for (int i = 0; i < numbersAsString.length; i++) { // perambulate the array numbersAsString with for cycle
- arrayOfParsedNumbers[i] = Integer.parseInt(numbersAsString[i]); // abstract the numbers from the array numbersAsStrings and parse them
- }
- int sumOfEvenNumbers = 0; // create variables sum to save the sums
- int sumOfOddNumbers = 0;
- for (int i = 0; i < arrayOfParsedNumbers.length; i++) { // perambulate the array arrayOfParsedNumbers with for cycle
- if (arrayOfParsedNumbers[i] % 2 == 0) { // if check - if the number of the array is even or odd
- sumOfEvenNumbers += arrayOfParsedNumbers[i]; //accumulate the even numbers
- } else {
- sumOfOddNumbers += arrayOfParsedNumbers[i]; // accumulate the odd numbers
- }
- }
- System.out.printf("The sum of even numbers is %d%n", sumOfEvenNumbers);
- System.out.printf("The sum of odd numbers is %d", sumOfOddNumbers);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment