Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Scanner is stated, meaning user can input values.
- */
- public class CountEven2 { //public class meaning accessible anywhere and specifically for named programs "CountEven2".
- private static Scanner sc;
- public static void main(String[] args) // public meaning accessible to the method, static meaning loading class into the memory, void meaning always returning something.
- {
- int Size, i = 0, j = 0, evenCount = 0; // initialized variable "size" and set variables i, j and evenCount to 0 for future use.
- sc = new Scanner(System.in);
- // Giving name to scanner "sc" and System.in to actually utilize it
- System.out.print(" Please Enter Number of elements in an array : "); // System prints this
- Size = sc.nextInt(); // Size is then stored into sc for future use
- 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
- System.out.print(" Please Enter " + Size + " elements of an Array : "); // System prints statement including the Size
- while (i < Size) // while loop is stated, will continue until Size is less than i or i is greater than size
- { // uses brackets to show what's going to specifically be in the while loop
- a[i] = sc.nextInt(); // takes the next int from the scanner and stores it in the array a at index "i"
- i++; // i is adding 1 until condition is false.
- }
- System.out.print("\n List of Even Numbers in this Array are :"); // System outputs statements after while loop ends
- while(j < Size) // another while loop is stated, this will run until "j" is larger than "Size" or "Size" is less than "j"
- {
- 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
- {
- System.out.print(a[j] +" "); // System prints "a[j]" after the process and adds a space.
- evenCount++; //evenCount is getting added by 1
- }
- j++; // This is after the while loop, j is added by one until while loop ends
- }
- 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.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement