0% found this document useful (0 votes)
17 views6 pages

عقد

1. The document provides examples and exercises to practice using while and do-while loops in Java programs. It includes code snippets to analyze and complete, as well as problems to solve like calculating sums and averages using loops. 2. The homework asks students to rewrite an exercise using a while loop instead of do-while, and to develop a program validating passwords with a do-while loop, prompting until a valid password over 8 characters is entered. 3. The document aims to help students develop programs using selection and iteration control structures like loops to implement control flow, as stated in its learning outcome.

Uploaded by

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

عقد

1. The document provides examples and exercises to practice using while and do-while loops in Java programs. It includes code snippets to analyze and complete, as well as problems to solve like calculating sums and averages using loops. 2. The homework asks students to rewrite an exercise using a while loop instead of do-while, and to develop a program validating passwords with a do-while loop, prompting until a valid password over 8 characters is entered. 3. The document aims to help students develop programs using selection and iteration control structures like loops to implement control flow, as stated in its learning outcome.

Uploaded by

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

CIS 1403: Fundamentals of Programming

LAB 4_Activity 2

Course Learning Outcome: CLO2


Develop programs using selection and iteration control structures to implement control flow.

Topic and Content:


 Develop programs using 'while' and 'do-while' loops to solve given problems.
Tools: repl.it

Version Author Effective Change Description DRC No


Date
1.0 Jumana Tarazi January 2024 1st Version 001

Page 1 of 6
Exercise 1- Apply

Select the correct answer:

1. Given the following Java code:


int i=1;
while (i < 4) {
System.out.print(i +" ");
i++;
}

What is the output after this code executes?


a) 1 2 3
b) 1 2 3 4
c) 1 2
d) 0 1 2

2. Given the following Java code:


int i=2;
while (i < 8) {
System.out.print(i +" ");
i+=2;
}

What is the output after this code executes?


a) 1 2 6 8
b) 2 4 6
c) 2 4 6 8
d) 4 6 8

Exercise 2 - Apply

Given the following Java code snippets, complete the missing code in the numbered blanks
(in red):

Java Code Missing code


1. //calculate and print the sum of even numbers from 2 to 1. 2
10 //using a 'while' loop 2. 10
3. sum

Page 2 of 6
int sum = 0; 4. i
int i = ___1___; 5. 2
while (i <= ___2___ ) {
sum = ___3____ +___4____ ;
i = i + ___5___ ;
}
System.out.println("Sum of even numbers: " + sum);

2. //calculate and print the product of odd numbers from 1 to 1. <=


7 //using a 'while' loop 2. product
3. i
int product = 1; 4. total
int i = 1; 5. i
while ( i __1__ 7 ) {
___2____* = ___3___ ;
i=i+2;
}
System.out.println("Product = " + product);

3. //calculate and print the average of the numbers from 1 to 1. <


5 //using a 'while' loop: 2. i
3. 1
int i=1; 4. total
double total = 0;
while ( i __1__ 6) {
total += ___2___;
i= i +__3__;
}
System.out.println("Average = " + ___4___ /5 );

5. //print the integers from 1 to 6, except for the number 5, 1. 7


//using a 'while' loop 2. !=
3. i
int sum = 0;
int i=1;
while ( i < ___1___ ) {
if ( i __2__ 5)
System.out.println(___3___ );
i++;
}

Page 3 of 6
Exercise 3 - Apply

Develop a Java program that uses a 'while' loop to ask the user to enter the price of 3 items
repeatedly one at a time. The program should calculate and display each price with VAT
included. The VAT rate is fixed at 5%.
Here is a sample run:
Enter price of item 1: 100
The price with vat included is: 105.0 AED
Enter price of item 2: 200
The price with vat included is: 210.0 AED
Enter price of item 3: 300
The price with vat included is: 315.0 AED

Import java util Scanner;


Public class main {
Public static void main ( string []args ) {
Scanner scanner = new scanner ( system.in);
Int conunt = 1
While ( count <=3) {
System . out. Print (“ enter price of item “ + count + “.”);
Double item price = scanner . nextDouble ();
// calculate the pric with vat included
Double price with vat = item price * 1.05 ;
System . out .println (“ the price with included is : ‘” + price with vat + “ AED”);
Count++;
}

Exercise 4 – Apply

Predict the output of the following Java code snippets:

Java Code Output

Page 4 of 6
2
3
4
5
6

4
3
2
1

Exercise 5 - Apply

Develop a Java program that uses a 'do-while' loop to repeatedly ask the user to enter an
integer. The loop should exit when the user enters the number 0.
Here is a sample run:
Enter a positive integer or 0 to quit: 1
Enter a positive integer or 0 to quit: 10
Enter a positive integer or 0 to quit: 0
Bye now

Import java . util . scanner ;


Public class main {
Pblic static void main ( string [] args ) {
Scanner scanner = new scanner ( system.in);
Int user input ;
Do {
System.out.print(“enter an integer ( enter 0 to exit ): “ );
User input = scanner . nextlnt ();
} while ( user input ! = 0 );
System . out . printin ( “ program exited . thank you !” )
}

Page 5 of 6
Homework - Apply

1. Rewrite the solution code for exercise 5 using a while loop instead of a do-while loop.
2. Develop a Java program that validates a user's password. The program should prompt the
user to enter a password, and then check whether it meets the following criterion:
o The password must be at least 8 characters long.

Use a do-while loop to repeatedly prompt the user for a password until a valid password is
entered. Provide feedback on whether the password is valid or invalid after each attempt.
Here is a sample run:
Enter a password: HH12345
Invalid password
Enter a password: gPkfrqanny
Invalid password
Enter a password: HH123456
Valid password

Page 6 of 6

You might also like