User Story
As a teacher of mathematics, I want to be able to quickly determine whether a set of numbers is even or odd, so that I can teach my students about number parity. When I input a list of integers into the system, the system should loop through each number in the list and determine whether the number is even or odd. If the number is even, the system should print a message to the console indicating that the number is even. If the number is odd, the system should print a message to the console indicating that the number is odd. By using this tool, I can easily demonstrate the concept of number parity to my students and help them understand how to classify numbers as even or odd.
Expected Outcome
2 is even.
4 is even.
6 is even.
8 is even.
10 is even.
1 is odd.
3 is odd.
5 is odd.
7 is odd.
9 is odd.
Code
List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (Integer num : numbers) {
if (num % 2 == 0) {
System.debug(num + ' is even.');
} else {
System.debug(num + ' is odd.');
}
}
In this example, we define a list of integers from 1 to 10 using the List<Integer>
syntax. We then use a for loop to iterate over each number in the list, storing the current number in the num
variable.
Within the for loop, we use an if statement to check if the current number is even or odd. We do this by using the modulus operator (%
) to check if the remainder of dividing the number by 2 is 0. If it is, the number is even and a message is printed to the debug log indicating that the number is even. If it is not, the number is odd and a message is printed to the debug log indicating that the number is odd.
Finally, the code prints the message to the debug log using the System.debug()
method.
You can adjust the values of the list to test the code with different sets of numbers.
Video
Video does not exists.