Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Main class of the Java program.
- */
- import java.util.Arrays;
- public class Main {
- public static void main(String[] args) {
- int[] test = new int[] {1, 2, 3, 4, 5};
- System.out.print("containsOdd(" + Arrays.toString(test) + ") = ");
- System.out.println(containsOdd(test, 0));
- }
- /**
- * containsOdd: must have an array of integers
- * postcondition: returns true if there is an odd number
- * */
- public static boolean containsOdd(int[] arr, int index) {
- if(index >= arr.length) //ends the loop when there is no more array
- {
- return false;
- }
- if(arr[index] % 2 != 0) //returns true if there is an odd number
- {
- return true;
- }
- return containsOdd(arr, index + 1); //restarts the situation with a higher index
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement