Sample Solution Comp 110 Practice Problems for Final Exam 1)Write the following 'while' loop using 'for' loop int count = 0; int sum = 0; while( count < 10 ) { sum += count; count++; } ******************************************************** int sum = 0; for(int count = 0; count < 10; count++ ) sum += count; 2) What is the output from each code segment? a) int sum = 0; for( int i = 0; i < 5; i++ ) { sum = sum + i; for( int j = 0; j < 5; j++ ) sum = sum + j; } System.out.println(sum); ******************************************************** ques2b: sum = 50 b) int sum = 0; for( int i = 0; i < 5; i++ ) { sum = sum + i; for( int j = i; j < 5; j++ ) sum = sum + j; } System.out.println(sum); ******************************************************** ques2b: sum = 50 3) Display the first num numbers in the Fibonacci sequence. The sequence starts with the following numbers: 1,1,2,3,5,8,12,21 After the initial two 1s, each number in the sequence is the sum of the two prvious numbers. You may assume num >= 2 public static void fibonacci(int num) { // Your code goes here } ******************************************************** public static void fibonacci(int num) { System.out.print("1,1"); int first = 1; int second = 1; int index = 3; while(index <= num) { int nextNumber = first + second; System.out.print("," + nextNumber); first = second; second = nextNumber; index++; } System.out.println(); } 4) Read a sequence of positive numbers from the keyboard. Display first the largest and then smallest number entered. Entering a negative number signals the end of the input. For example, the input sequence 13 12 66 91 -999 produces the output 12 91 ******************************************************** public static void ques4() { Scanner inp = new Scanner(System.in); int largest = -1; int smallest = 99999999; System.out.println("enter next number: " ); int nextNumber = inp.nextInt(); while(nextNumber >0 ) { if(nextNumber > largest) largest = nextNumber; else if(nextNumber < smallest) smallest = nextNumber; System.out.println("enter next number: " ); nextNumber = inp.nextInt(); } System.out.println("smallest = " + smallest + " largest = " + largest); }