0% found this document useful (0 votes)
8 views

CS110T-Lab7-student-1446 wed

The document outlines Lab 7 for CS110T: Programming Language, focusing on loops in Java. It includes exercises for reading user input, implementing while and do-while loops, and writing programs to solve specific problems. The lab aims to enhance students' understanding of loops through practical coding tasks and examples.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CS110T-Lab7-student-1446 wed

The document outlines Lab 7 for CS110T: Programming Language, focusing on loops in Java. It includes exercises for reading user input, implementing while and do-while loops, and writing programs to solve specific problems. The lab aims to enhance students' understanding of loops through practical coding tasks and examples.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS110T: Programming Language1

Lab 7: Loops I

Lab Objectives:
In this lab, the student will practice:
✔ Reading data values from the user.
✔ Compiling and executing Java applications.
✔ While loop and do-while loop.
Lab Exercise 1: Program Output
What is the output of the following codes?

a)
public class Lab6 {
public static void main(String[] args) {
int count = 1;
while (count <= 10)
{
count *= 2;
}
count = count - 10;
System.out.println(count);

}
}
b)
public class Lab6 {
public static void main(String[] args) {
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
}
}
c)
public class Lab6 {
public static void main(String[] args) {
boolean flag = true;
int count = 0;

do {
System.out.print(count + " ");
count++;
if (count == 5)
flag = false;

} while (flag);
}
}

2
Lab Exercise 2: Code writing (1)
Problem Description: Write a Java program that asks the user to input letters one by
one and builds a word from those letters. The program should stop asking for more
letters when the user types a dot (.), and then print the completed word. Use while
loop.
Output:
Enter a letter or a dot to stop: h
Enter a letter or a dot to stop: e
Enter a letter or a dot to stop: l
Enter a letter or a dot to stop: l
Enter a letter or a dot to stop: o
Enter a letter or a dot to stop: .
The word you formed is: hello

Lab Exercise 3: Code writing (2)


a) Problem description: Write a Java program that prompts the user to enter the names of
books they have read. The program will repeatedly ask the user to input book names
unless the user types "done" to stop. Use a while loop.
• Form and display a complete sentence listing all the books entered, with proper
punctuation. If only one book is entered, it should display that book directly. If
multiple books are entered, the sentence should include commas.
• Display the total number of books entered (excluding "done").
Output Sample:
Enter a book name (or type 'done' to finish): The Alchemist
Enter a book name (or type 'done' to finish): 1984
Enter a book name (or type 'done' to finish): The Great Gatsby
Enter a book name (or type 'done' to finish): done
You have read The Alchemist, 1984, The Great Gatsby.

b) Problem description: Write a Java program that prompts the user to enter their daily
expenses in SR. The program will keep asking the user to enter an expense unless they
enter -1 to stop. Use a while loop to implement this. Once the user finishes entering the
expenses, the program should:

• Calculate and display the total expense amount.


• Display the average expense per day.

3
Output Sample:
Enter your daily expense in SR (enter -1 to stop): 50.75
Enter your daily expense in SR (enter -1 to stop): 25.30
Enter your daily expense in SR (enter -1 to stop): 10.00
Enter your daily expense in SR (enter -1 to stop): 100.50
Enter your daily expense in SR (enter -1 to stop): -1
Summary of Expenses:
- Total Expenses: 186.55 SR
- Expenses Average: 46.64 SR

c) Problem description: Write a java program that ask the user to enter the number of
product items, then enters the price for each product, then the system will calculate the
discount and print the product price after the discount. Use while loop to do this code.
• The DiscountRate is 10% (or 0.1).
• Total price after the discount = total price * DiscountRate
• Terminate the loop when the user enters 0.
Output Sample:
Enter the number of items you want to purchase: 2
Enter the price for item 1 or 0 to exit: 30
Enter the price for item 2 or 0 to exit: 70
Enter the price for item 2 or 0 to exit: 0
The total price after discounts is: $90.0

4
LAB 7 Assignment Questions
A) Write java program that prompts the user to enter the number they wanted to calculate the
summation for (Hint: for example, ‘summation 3 equals to 3+2+1’).
• The entered number must be more than 0.
• If the summation is odd, then the system must print “The sum is odd.”. else, the system
must print “The sum is even.”.

Output Sample:
Enter the limit (greater than 0): 5
Summation: 15
The sum is odd.

B) Write java program that shows the user a list of calculation operation, after the user choose one
from the list, the system will ask the user to enter the numbers to calculate the chosen
operation.
• The while loop will have the next mathematical operations:
1. Addition.
2. Subtraction.
3. Multiplication.
4. Division.
• If the user enters (5), then the system will exit from the program.
Use the while and switch to do the program.

Output Sample:
Choose from the list:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice (1-5): 3
Enter first number: 3
Enter second number: 7
Result: 21.0

Choose from the list:


1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice (1-5): 5
Exiting the program.

You might also like