6 - Loops - Examples
6 - Loops - Examples
Exercise (1):
int i = 1;
(i < 6) {
System.out.println(i);
;
}
Exercise (2):
int i = 1;
{
System.out.println(i);
i++;
}
(i < 6);
2)
public static void main(String[] args) {
int i = 0;
int s = 0;
while (i < 5){
i++;
s += i;
}
System.out.println("The result is:"+s);
}
………………………………………………………………………………………………
3)
public static void main(String[] args) {
int i = 0;
while (i < 5){
i++;
System.out.println(++i);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
4)
public static void main(String[] args) {
int x = 5;
while (x < 10){
--x;
System.out.print(x + " ");
}
System.out.println (x);
}
………………………………………………………………………………………………
5)
public static void main(String[] args) {
int count = 0;
do {
System.out.println("Welcome to Java "+ count);
} while (count++ < 5);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
6)
public static void main(String[] args) {
int x = 1;
do {
x = x + x;
System.out.print (x + " " );
} while (x < 5);
}
………………………………………………………………………………………………
7)
public static void main(String[] args) {
int x=6, y = 1 , z = 0;
do {
y = y * 2;
z++;
} while (2 * y <= x);
System.out.print (y + " " + z);
}
………………………………………………………………………………………………
8)
public static void main(String[] args) {
int x=25, y=2, z = 0;
do {
x = x / y;
z++;
System.out.print (x + ", ");
} while (x % y != 0);
System.out.print (z);
}
………………………………………………………………………………………………
9)
public static void main(String[] args) {
int j = -5;
do {
System.out.print("\n"+ j);
j = j + 1;
} while(j <= 0);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
10)
public static void main(String[] args) {
int i = 10;
do{
System.out.print(i + "\t");
i-=3;
} while (i >=0);
}
………………………………………………………………………………………………
11)
public static void main(String[] args) {
int x=0;
do{
x = x + 10;
System.out.print(" "+ x);
} while ( x <= 50 );
12)
public static void main(String[] args) {
int x=7, sum=0;
do{
sum+=x;
x--;
}while(x<5);
System.out.println("sum = " + sum);
}
………………………………………………………………………………………………
*************************************************************
C) Find the errors and correct them:
1)
public static void main(String[] args) {
int j=0;
do {
System.out.print("\t"+j);
} while (j < 10);
}
………………………………………………………………………………………………
2)
public static void main(String[] args) {
int i=100 ;
while( i>=1)
System.out.println( i );
++i;
}
………………………………………………………………………………………………
3)
public static void main(String[] args) {
c=5;
do{
System.out.println( c );
c+=5;
}while( c<=10);
}
………………………………………………………………………………………………
4)
public static void main(String[] args) {
int count=16;
do{
System.out.print(" " + count);
count += 4;
}while(count>=0);
}
………………………………………………………………………………………………
5)
public static void main(String[] args) {
int c=10;
int product = 1;
do{
product *= c;
c -= 5;
}while(c >= 5)
System.out.print("Product " + product);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
*************************************************************
8
11
14
17
20
23
2. Write a program that reads 10 number then prints out the number of
even and odd numbers.
3. write a program that read a number and print the timetable of that
number.
4. Write a Java program to find the sum of first 10 natural numbers.
Expected Output :
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
D) Write a Java program: do while loop
*****
*****
*****
*****
*****
8. Write a program in Java to display the n terms of odd natural number and
their sum.
Test Data
Input number of terms : 10
Expected Output :
The odd numbers are :1 3 5 7 9 11 13 15 17 19
The Sum of odd Natural Number upto 10 terms : 100