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

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)
9 views

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

Use a for loop to print "Yes" 5 times:

(int i = 0; i < 5; ) {
System.out.println( );
}

Exercise (2):

Stop the loop if i is 5.

for (int i = 0; i < 10; i++) {


if (i == 5) {
;
}
System.out.println(i);
}

Exercise (3):

In the loop, when the value is "4", jump directly to the next value.

for (int i = 0; i < 10; i++) {


if (i == 4) {
;
}
System.out.println(i);
}
B) What is the output of the following fragments:
1)
public static void main(String[] args) {
for (int n = 0; n<5; n++)
System.out.print(n + " ");
}
………………………………………………………………………………………………
2)
public static void main(String[] args) {
int i;
for (i=0; i<10; i++){
System.out.print("For statement ");
System.out.println(i);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
public static void main(String[] args) {
int i;
for (i=0; i<5; i++)
System.out.println("Value of i: "+ i);

System.out.println("Press Enter Key to Exit..");


}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
4)
public static void main(String[] args) {
int i, sum=0;
for(i=1; i<=5; i++)
sum += i;
System.out.println("Sum = "+ sum);
}
………………………………………………………………………………………………
5)
public static void main(String[] args) {
for (int c=10; c>=0; c-=2)
System.out.print(c + " ");
}
………………………………………………………………………………………………
6)
public static void main(String[] args) {
for (int c=50; c>=10; c-=5)
System.out.print(c + " ");
}
………………………………………………………………………………………………
public static void main(String[] args) {
for (int c=1; c<=26; c*=3)
System.out.print(c + " ");
}
………………………………………………………………………………………………
7)
public static void main(String[] args) {
for (int c=10; c>=1; c/=2)
System.out.print(c + " ");
}
………………………………………………………………………………………………
7)
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print (i * j + "\t");
}
System.out.println ();
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
8)
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print ("#");
}
System.out.println();
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

9)
public static void main(String[] args) {
int i;
for(i = 3; i != 2; i = i - 1)
System.out.print(i + " ");
}
………………………………………………………………………………………………

10)
public static void main(String[] args) {
for (int i = 0; i <= 100; i += 10){
if (i == 20)
break;
System.out.print(i + "\t");
}
}
………………………………………………………………………………………………

11)
public static void main(String[] args) {
for (int c = 20; c <= 80; c += 20){
if (c == 40)
continue;
System.out.print(c + " ");
}
}
………………………………………………………………………………………………
12)
public static void main(String[] args) {
for (int count = 1; count <= 6; count++ ){
if ( count %2==0 )
continue;
System.out.print(count+" ");
}
}
………………………………………………………………………………………………
13)
public static void main(String[] args) {
int c = 1;
while ( c <= 10){
if ( c == 5 ){
c+=2;
continue;
}
System.out.print(c+" ");
c++;
}
}

*************************************************************
C)
1) How many times is the body of the loop below executed?
int counter;
for (counter =1; counter > 20; counter++)
System.out.println("loop body");
………………………………………………………………………………………………

2) What is the different between the outputs of the following two


segments of code ?

public static void main(String[] args) { public static void main(String[] args) {
for (int i = 1; i <= 5; i++) { for (int i = 1; i <= 5; i++)
System.out.print('@'); System.out.print('@');
System.out.print('*'); System.out.print('*');
System.out.println(); System.out.println();
}
}

3)
public static void main(String[] args) {
for ( )
System.out.println();

}
Modify the code above to produce the following output:
2 times 1 = 2
2 times 2 = 4
2 times 3 = 6
2 times 4 = 8
*************************************************************
D) Find the errors and correct them:

1)
public static void main(String[] args) {
for (int r = 0; r <= 9; r += 3)
System.out.println(" \t"+ r);
System.out.print(" "+ r);
}
………………………………………………………………………………………………

2)
public static void main(String[] args) {
for(i=100 ; i>=1 ; i-- )
System.out.println( i );
}
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
public static void main(String[] args) {
for(int c=1 ; c<=10 ; c-- )
System.out.println( c );
}
………………………………………………………………………………………………
4)
public static void main(String[] args) {
for (int count = 1; count <= 6; ){
System.out.print(count+" ");
}
}
………………………………………………………………………………………………

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

E) Write a Java program: for loop


1. Write a program in Java to display the multiplication table of a given
integer.
Test Data :
Input the number (Table to be calculated) : 15
Expected Output :
15 X 1 = 15
...
...
15 X 10 = 150
2. Write a program to print the even numbers from 0 to 100. Print each
number per line.

3. Write a program in Java to display the first 10 natural numbers.


Expected Output :
1 2 3 4 5 6 7 8 9 10

4. Write a program in Java to display n terms of natural number and their sum.

Test Data : 7
Expected Output :
The first 7 natural number is :
1234567
The Sum of Natural Number up to 7 terms : 28

5. Write a program in Java to read 10 numbers from keyboard and find their
sum and average.
Test Data :
Input the 10 numbers :
Number-1 :2
...
Number-10 :2
Expected Output :
The sum of 10 no is : 51
The Average is : 5.100000

E) Write a Java program: nested for loops


1. Write a program to print the following

$$$$$
$$$$$
$$$$$
$$$$$
$$$$$

2. Write a program to print the following


****
***
**
*

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy