You are on page 1of 2

Programming Lab II (CSBP221)

Fall 23

Lab 2

Objective and Overview:


 To use repetetion statements.

CLO: This lab maps to CLO #1


Instructions:
 The first set of exercises is to be solved together in the lab with the instructor.
 The second set of exercises is to be solved by students. Students may work in groups of 2.
 All lab exercises must be submitted by the end of each lab (one submission per group).

SET: 1

Exercise #1: Write a Java program using for loop to print out “Hello World!” 25 times.

Exercise #2: Write a Java program using while loop to print out “Hello World!” 25 times.

Exercise #3: Write a Java program using for loop to find out the sum and the average of all
odd integers between 1 and 100.

Exercise#4: Write a java program that reads from the user up to 10 numbers, using while loop
with a SENTINEL, to count and add the positive values and then display the results. Your
program should terminate whenever it either reads 10 number or a any negative number.

Here is a smaple run:


2 7 -6
Number of positive numbers is: 2
The sum of the positive numbers is: 9

Another sample run:


2 7 6 10 3 9 5 9 4 2
Number of positive numbers is: 10
The sum of the positive numbers is: 57

SET 2:

Exirces#1: Salma learned recently about prime numbers. She can determine whether a number
is prime or not for small numbers, however, for large numbers she needs to make some
calculations. Salma asks you to write a java program to verify if a given number is "Prime" or
"not Prime". Remember that she may use your program for very large numbers.

1/2
Hint: the simplest way to determine if a number (N) is prime or not is to check if the numbers
from 2 to N-1 divide N without a remainder. (Moreover, stop at N/2 rather than N-1 is enough).

2/2

You might also like