Professional Documents
Culture Documents
Objectives:
By completing this lab, students should be able to:
Learn different kind of loop concept (while, do-while, andfor)
Learn how to use the nestedloop.
do{ System.out.println(start);
Statement_1; start= start +10 ;
Statement_2;
... }while(start< =30);
Update;
}while(Boolean_Expression) The output:
10
20
30
Example (2):
int start = 10;
while (start< =40){
System.out.print(i +” “);
start = start +10 ;
}
The output:
102030 40
Example (3):
int rowNum, columnNum;
for (rowNum = 1; rowNum<=3; rowNum++)
{
for (columnNum = 1; columnNum<=2;columnNum++)
System.out.print(" row " + rowNum+ "column "+ columnNum);
System.out.println();
}
The output:
Exercise 1:
1. Create class named itLab6Exercise1
2. Write a java program by using a while-loop to display the first 24 numbers starting from6.
Solution:
public class Lab6Exercise1{
public static void main (String [ ]args) {
int j=6;
while (j<=24){
System.out.println(j);
j++;
}
}}
Exercise 2:
1. Create class named itLab6Exercise2
2. Write a java program by using a do-while loop to display the numbers from32 , 28, 24to 6.
Solution:
public class Lab6Exercise3{
public static void main (String [ ]args) {
int sum=0;
for(int i=2; i<=60;
i=i+2){sum= sum+i;}
System. out .println("The sum of even numbers between 2 and 50 is: "+
sum);}}
Exercise4:
1. Create class named itLab6Exercise4
2. Write a java program that will display the following:
Exercise 5:
1. Create class named itLab6Exercise5
2. Write a program that reads an alphabetic character and a number of repeat
then prints a triangle of alphabet. Example:
t
tt
ttt
tttt
ttttt
Solution:
import java.util.Scanner;
class Lab {
public static void main(String[] args) {
System.out.println("Enter Char");
Scanner in=new Scanner(System.in);
String a=in.next();
Char x= a.charAt(0);
System.out.println ("Enter number");
int n=in.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print (a);}
System.out.println(); } }}