0% found this document useful (0 votes)
22 views7 pages

6 - Loops - Examples

Uploaded by

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

6 - Loops - Examples

Uploaded by

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

A) Java Exercises:

Exercise (1):

Print i as long as i is less than 6.

int i = 1;
(i < 6) {
System.out.println(i);
;
}

Exercise (2):

Use the do/while loop to print i as long as i is less than 6.

int i = 1;
{
System.out.println(i);
i++;
}
(i < 6);

B) What is the output of the following fragments:


1)
public static void main(String[] args) {
int i = 7;
while (i < 28){
System.out.print(i + "\t");
i+=7;
}
}
………………………………………………………………………………………………

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 );

System.out.println(" " + x);


}
………………………………………………………………………………………………

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);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

*************************************************************

D) Write a Java program: while loop


1. Write a complete Java program that produces this sequence of
numbers using a while loop:

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

5. Write a program to print out all integers from 5 down to -5.


6. Write a necessary Java program that reads (10) numbers from a user,
then find and print the smallest number among them.

7. Write a program to print the following

*****
*****
*****
*****
*****
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

You might also like