Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Scanner is stated, meaning user can input values.
  5. */
  6. public class CountEven2 { //public class meaning accessible anywhere and specifically for named programs "CountEven2".
  7. private static Scanner sc;
  8. public static void main(String[] args) // public meaning accessible to the method, static meaning loading class into the memory, void meaning always returning something.
  9. {
  10. int Size, i = 0, j = 0, evenCount = 0; // initialized variable "size" and set variables i, j and evenCount to 0 for future use.
  11.  
  12. sc = new Scanner(System.in);
  13. // Giving name to scanner "sc" and System.in to actually utilize it
  14. System.out.print(" Please Enter Number of elements in an array : "); // System prints this
  15. Size = sc.nextInt(); // Size is then stored into sc for future use
  16.  
  17. int [] a = new int[Size]; // Array is used, integer will be the data type, "[]" tells the programs that it's going to use an array, "a" is the variable, new int meaning new integer to utilize and finally "[size]" which is the amount of values stored in the array
  18.  
  19. System.out.print(" Please Enter " + Size + " elements of an Array : "); // System prints statement including the Size
  20. while (i < Size) // while loop is stated, will continue until Size is less than i or i is greater than size
  21. { // uses brackets to show what's going to specifically be in the while loop
  22. a[i] = sc.nextInt(); // takes the next int from the scanner and stores it in the array a at index "i"
  23.  
  24. i++; // i is adding 1 until condition is false.
  25. }
  26. System.out.print("\n List of Even Numbers in this Array are :"); // System outputs statements after while loop ends
  27. while(j < Size) // another while loop is stated, this will run until "j" is larger than "Size" or "Size" is less than "j"
  28. {
  29. if(a[j] % 2 == 0) //if the int stored in a[j] with 2 is equal to zero basically if it divided by 2 has a remainder of zero in this case this means an even number
  30.  
  31. {
  32. System.out.print(a[j] +" "); // System prints "a[j]" after the process and adds a space.
  33. evenCount++; //evenCount is getting added by 1
  34. }
  35. j++; // This is after the while loop, j is added by one until while loop ends
  36. }
  37.  
  38. System.out.println("\n Total Number of Even Numbers in this Array = " + evenCount); // Ending where system prints the statement included with outcome of evenCount after process.
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement