0% found this document useful (0 votes)
7 views2 pages

Class IX - Nested Loop - Program 01 (Pattern 01)

The document provides a Java program to display a nested loop pattern of asterisks. It includes the code implementation, a description of how the nested loops work, and a dry run example with an input of 5 rows. The output demonstrates the incremental display of asterisks in each row.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Class IX - Nested Loop - Program 01 (Pattern 01)

The document provides a Java program to display a nested loop pattern of asterisks. It includes the code implementation, a description of how the nested loops work, and a dry run example with an input of 5 rows. The output demonstrates the incremental display of asterisks in each row.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

K.E.

CARMEL SCHOOL, AMTALA


COMPUTER
CLASS–IX NESTED LOOP PROGRAM–01

Nested Loop (Pattern 01)


Write a program in Java to display the following pattern :
*
* *
* * *
* * * *
* * * * *

Code
import java.io.*;
import java.lang.*;
import java.util.*;
class Pattern1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows : ");
int r = sc.nextInt();

for(int i=1; i<=r; i++) // to count the number of rows


{
for(int j=1; j<=i; j++) // to display the pattern
{
System.out.print("*");
}// end of inner for
System.out.println(); // to move to the next row
}// end of outer for
}// end of main
}// end of class
Output

Dry Run
Let number of rows = 5, i=1 (initially), (i*j) number of times program will execute
for(int i=1; i<=r; i++)
for(int j=1; j<=i; j++)
outer for loop starts….
i=1, 1<=5 True, inner for loop starts…. j=1, 1<=1, True *
j=2, 2<=1, False inner for loop ends….
i=2, 2<=5 True, inner for loop starts…. j=1, 1<=2, True *
j=2, 2<=2, True **
j=3, 3<=2, False inner for loop ends….
i=3, 3<=5 True, inner for loop starts…. j=1, 1<=3, True *
j=2, 2<=3, True **
j=3, 3<=3, True ***
j=4, 4<=3, False
i=4, 4<=5 True, inner for loop starts…. j=1, 1<=4, True *
j=2, 2<=4, True **
j=3, 3<=4, True ***
j=4, 4<=4, True ****
j=5, 5<=4, False inner for loop ends….
i=5, 5<=5 True, inner for loop starts…. j=1, 1<=5, True *
j=2, 2<=5, True **
j=3, 3<=5, True ***
j=4, 4<=5, True ****
j=5, 5<=5, True *****
j=6, 6<=5, False inner for loop ends….
i=6, 6<=5 False, outer for loop ends….

You might also like