CS 1713 Introduction to Computer Science, Spring 2008 Quiz 2 Comments
20 people took this quiz. Seven people got 1 point and 3 people got 2 points.
-
Write a program segment that increments the integer variable
count if the integer variable value is 2 or 3.
- Some people confused && and ||
if ( (value == 2) || (value == 3))
count++;
-
Write a program segment that uses a loop to set the double variable
sum to the
sum of the square roots of the integers between 1 and 1000, inclusive.
Hint: use Math.sqrt().
- A for loop is best for this.
- sum needs to be initialized (but you do not need to declare it).
- The loop index needs to be declared.
- square and square root are different.
sum = 0;
for (int i=1; i<=1000; i++)
sum = sum + Math.sqrt(i);