Lab5 SE 120L Spring 2024
Lab5 SE 120L Spring 2024
Lab5 SE 120L Spring 2024
COLLEGE OF ENGINEERING
SOFTWARE ENGINEERING
Objectives:
• To familiarize oneself with the for, while and do...while loops as fundamentally important
programming tools in all languages in general and Java in particular.
• To understand the individual components making up the loop statement for each loop type
and how the block of code inside the for loop will be executed.
Notes:
• All Java code is to be written under Windows using available lab computer systems.
• This lab is worth 30 marks. When you finish an exercise, ask the Lab Instructor to check your
work.
Page 1 of 7
Exercise #1 (10 Marks):
Write a program that sums up the squares and the cubes of integers from 1 to n, where n is
entered by the user. Use a for loop to solve this exercise.
How many integers you want to compute for their squares and cubes: 7
The sum of squares is: 140
The sum of cubes is: 784
Copy and paste the code of your program for Exercise 1 here:
Paste the screenshot of your program and your output for Exercise 1 here:
Page 2 of 7
Exercise 2 (10 Marks):
Write a program that prompts a student to enter a score. If the score is greater or equal to 60, display
“You pass the exam”; otherwise, display “You did not pass the exam”. Your program ends when the
input is a negative number and display “Invalid score”. Use a while loop to solve this exercise.
Copy and paste the code of your program for Exercise 2 here:
import java.util.Scanner;
while (true) {
// Prompt the user to enter a score
System.out.print("Enter a score (or a negative number to exit): ");
int score = scanner.nextInt();
Page 3 of 7
if (score < 0) {
System.out.println("Invalid score. Exiting the program.");
break; // Exit the loop if the input is negative
}
Paste the screenshot of your program and your output for Exercise 2 here:
Page 4 of 7
Exercise #3 (10 Marks):
Write a program that displays all even number from 1 to 10 then finds the sum and. Your program
should also find how many numbers are greater, smaller, and equal to the calculated average. Use
do ... while loop to solve this exercise.
Copy and paste the code of your program for Exercise 3 here:
import java.util.Scanner;
int sum = 0;
int count = 0;
int greaterCount = 0;
int smallerCount = 0;
Page 5 of 7
int number = 2;
do {
System.out.println(number);
sum += number;
number += 2;
count++;
Paste the screenshot of your program and your output for Exercise 3 here:
Page 6 of 7
Page 7 of 7