HW0330 - if(number<10) return number; return...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
//Xin Chi comp401 Tue-THur 11:00 import static java.lang.System.out; import java.util.Arrays; public class HW0330 { public static int[] x = new int[] { 10 , 30, 70, 44, 3 }; public static void main(String[] args) { out.println("number of odd digits in 987 = " + problem3(987)); out.println("sum of digits in 987 = " + problem4(987)); out.println("sum of values in x = " + problem6(x, 0)); out.println("largest value in x = " + problem7(x, 0)); out.println(Arrays.toString(problem9(x,0))); } public static int problem3(int number) { if(number<10) return number%2; return problem3(number/10)+number%2; } public static int problem4(int number){
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if(number<10) return number; return problem4(number/10)+ number%10; } public static int problem6(int array, int index){ if(array.length == index+1) return array[index]; return problem6(array,index+1) + array[index]; } public static int problem7(int array, int index){ if(array.length == index+1) return array[index]; return Math.max(problem7(array,index+1), array[index]); } public static int problem9(int array, int index){ if(array.length == index) return array; else if(index > 0) array[index] += array[index-1]; problem9(array,index+1); return array; } }...
View Full Document

This note was uploaded on 06/10/2011 for the course COMP 110 taught by Professor Stotts during the Spring '08 term at UNC.

Ask a homework question - tutors are online