Lab5 SE 120L Spring 2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ALFAISAL UNIVERSITY

COLLEGE OF ENGINEERING
SOFTWARE ENGINEERING

SE 120: Object Oriented Programming I


Spring 2024
Lab 5
ITERATIONS STATEMENTS AND LOOPS
Name: karam Date:2/20/2024
Student ID:240419 Time and day:3:30

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.

Here is a sample run:

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.

Here is a sample run:

Enter your score: 85.5


You pass the exam

Enter your score: 52.0


You did not pass the exam

Enter your score: -1


Invalid score

Copy and paste the code of your program for Exercise 2 here:

import java.util.Scanner;

public class ExamResult {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

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
}

if (score >= 60) {


System.out.println("You pass the exam");
} else {
System.out.println("You did not pass the exam");
}
}

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.

Here is a sample run:

The even numbers from 1 to 10 are:


2
4
6
8
10
The sum of even number is: 30
The average of even number is: 6.0
Number of even numbers greater than the average is: 2
Number of even numbers less than the average is: 2
Number of even numbers equal to the average is: 1

Copy and paste the code of your program for Exercise 3 here:

import java.util.Scanner;

public class EvenNumbersAnalysis {


public static void main(String[] args) {

int sum = 0;
int count = 0;
int greaterCount = 0;
int smallerCount = 0;

// Create a Scanner object for user input


Scanner scanner = new Scanner(System.in);

Page 5 of 7
int number = 2;
do {
System.out.println(number);

sum += number;

int average = (count != 0) ? (sum / count) : 0;

if (number > average) {


greaterCount++;
} else if (number < average) {
smallerCount++;
}

number += 2;
count++;

} while (number <= 10);

System.out.println("Sum of even numbers: " + sum);

// Display the analysis


System.out.println("Numbers greater than the average: " +
greaterCount);
System.out.println("Numbers smaller than the average: " +
smallerCount);
System.out.println("Numbers equal to the average: " + (count -
greaterCount - smallerCount));
}
}

Paste the screenshot of your program and your output for Exercise 3 here:

Page 6 of 7
Page 7 of 7

You might also like