Advertisement
Kancho

Simple_Sum_EVEN_ODD_Array

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