Pattern Program - 2 in JAVA
Pattern Program - 2
Program to display the following number pattern on screen.
Pattern :-
12345
1234
123
12
1
12
123
1234
12345
1234
123
12
1
12
123
1234
12345
Program is divided into two parts
- Make loop in descending order to print pattern till 1 i.e.
12345
1234
123
12
1
1234
123
12
1
2. Then make loop in ascending order to print the remaining elements i.e.
12
123
1234
12345
3. Now Compile and run the program.
Program below :-
class Pattern
{
public static void main(String args[])
{
int i,j;
for(i=5;i>=1;i--) // Decrement Loop to print pattern in descending order
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
for(i=2;i<=5;i++) // increment loop to print the pattern then in ascending order
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
} // end of main method
} // end of class
{
public static void main(String args[])
{
int i,j;
for(i=5;i>=1;i--) // Decrement Loop to print pattern in descending order
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
for(i=2;i<=5;i++) // increment loop to print the pattern then in ascending order
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
} // end of main method
} // end of class
Comments
Post a Comment