Sunday, February 3, 2013

Java, Nested For Loops, To Generate Patterns

Java, Nested For Loops, To generate.

I use to for loops to generate the following output :


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Step 1 ; Use for to generate the five rows,

Step 2 ; For each row, generate the numbers.

The code as stated below :


//Java Nested Loops

/* Program to generate the following output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

*/

public class NestedLoop {
public static void main(String[] args){

// I use two nested for loop to generate the pattern
// to generate 5 rows
for(int i = 1; i <= 5 ; i++){

//to generate the numbers
for(int j = 1 ; j <= i ; j++){
System.out.print(j + " ");
}

System.out.println(" ");
}

}
}




No comments:

Post a Comment