Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. /**
  2. * Main class of the Java program.
  3. */
  4. import java.util.Arrays;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. int[] test = new int[] {1, 2, 3, 4, 5};
  10.  
  11. System.out.print("containsOdd(" + Arrays.toString(test) + ") = ");
  12. System.out.println(containsOdd(test, 0));
  13. }
  14. /**
  15. * containsOdd: must have an array of integers
  16. * postcondition: returns true if there is an odd number
  17. * */
  18. public static boolean containsOdd(int[] arr, int index) {
  19. if(index >= arr.length) //ends the loop when there is no more array
  20. {
  21. return false;
  22. }
  23. if(arr[index] % 2 != 0) //returns true if there is an odd number
  24. {
  25. return true;
  26. }
  27. return containsOdd(arr, index + 1); //restarts the situation with a higher index
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement