College of Computer and Information Sciences

Total Page:16

File Type:pdf, Size:1020Kb

College of Computer and Information Sciences

King Saud University College of Computer and Information Sciences Information Technology Department

First Semester 1430/143129 CSC 111: Introduction to Programming with Java TUTORIAL # 5 Chap5: Control Structure II PART I Q1. Trace the following program: public static void main( String args[] ) { int num; int count; int sumFactors; num = 1; count = 0; while (count < 4) { sumFactors = 0; for (int i = 1; i <= (num / 2); i++) if (num % i == 0) sumFactors = sumFactors + i; if (sumFactors == num) { System.out.println(num + " is a perfect integer."); count++; } num++; } } for ( i = 1; i <= 5; i++ ) { for ( j = 1; j <= 3; j++ ) { for ( k = 1; k <= 4; k++ ) System.out.print( '*' ); System.out.println(); } // end inner for System.out.println(); } // end outer for

1 int i ,c=12; for (i=1; i<4;i++) switch( i %3) { case 0 : System.out.printf("%d",i*i); break; case 1: c=c==c !=c; case 2: System.out.printf("%d %d",c,i); } int x,n=5,i=0; double p =0; while ( n != 0) { x=n % 2; n = n/2; if (x ==0) continue; i +=1; p+= x*Math.pow(10,i); } System.out. printf("%.2f",p/10);

2 Q2. Find and correct the errors in the following segments: a. while(c<=5){ p *=c; --c; }

b. i=0; do{ System.out.printf(“%d\n”, i ); }while(i<0);

c. for ( x = 100,x >=1;x++) System.out.printf(“%d\n”,x);

d. the following code should output odd integers from 999 to 1: for (x=999;x>=1;x+=2) System.out.printf(“%d\n”,x);

Q3.

3 Q4. Convert the following code so that it uses nested while statements instead of for statements: int s = 0; int t = 1; for (int i = 0; i < 10; i++) { s = s + i; for (int j = i; j > 0; j--) { t = t * (j - i); } s = s * t; System.out.println(“T is “ + t); } System.out.println(“S is “ + s);

Q5. Write a program that output 20 random numbers from 1 to 6. Print only five integers per line (Note: use Dialog box for Output). Q6. Write an application that calculates the product of the odd integers from 1 to 15. Q7. One interesting application of computers is to display graphs and bar charts. Write an application that reads five numbers between 1 and 30. For each number that is read, your program should display the same number of adjacent asterisks. For example, if your program reads the number 7, it should display *******. Q8. Write a while loop that sums the integers from 1 to 10, excluding 3 and 6. Print the sum. Q9. Write a do… while loop that prints the integers from 10 to 0, inclusive.

4 PART II Q10. To make telephone numbers easier to remember some companies use letters to show their telephone number. For example the telephone number 438-5626: can be shown as GET-LOAN. In some cases to make a telephone meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL-HOME, which uses eight letters. Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than eight letters then process only the first seven letters. Also, output the - (hyphen) after the third diet. Allow the user to use uppercase and lowercase letters as well as spaces between words. Moreover, your program should process as many telephone numbers as the user wants. Q11. Write a loop that will create a new string that contains every other character in a given string. Q12. Write a fragment of code that will read words from the keyboard until the word done is entered. For each word except done, report whether its first character is equal to its last character. For the required loop, use a a. while statement b. do-while statement

Q13. Suppose that we want to compute a weighted average of a list of numbers. The first number will have a weight of 1, the second will have a weight of 2, the third will have a weight of 3, and so on. The weighted average is the sum of the weights times the values divided by the sum of the weights. For

1  5  2  8  3  9  4  3  5 13 example, if the list is 5, 8, 9, 3, 13, the weighted average would be . 1  2  3  4  5 Use a loop with a sentinel value to allow a user to enter an arbitrary number of values. Compute and display the weighted average of all the values, excluding the sentinel.  Q14.

5

Recommended publications