C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Pattern Program - Java Programs

Write two separate programs to generate the following patterns using iteration (loop) statements:
(a)

*
*#
*#*
*#*#
*#*#*

(b)

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

Pattern Program - 1

class Pattern1{ public static void main(String args[]){ for(int i = 1; i <= 5; i++){ char ch = '*'; for(int j = 1; j <= i; j++){ System.out.print(ch); if(ch == '*') ch = '#'; else ch = '*'; } System.out.println(); } } }

Output

 
 OUTPUT : 
*
*#
*#*
*#*#
*#*#*
 

Pattern Program - 2

class Pattern2{ public static void main(String args[]){ for(int i = 1; i <= 5; i++){ for(int j = 5; j >= i; j--) System.out.print(j); System.out.println(); } } }

Output

 
 OUTPUT : 
54321
5432
543
54
5