Print Number Pattern in Java


Prev Tutorial Next Tutorial

Java Program to Print Number Pattern

In Java language you can easily print number pattern using for loop and also using while loop, here i will show you in simple way to print these all patterns.

Print Number Pattern in Java

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

Output

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

Print Number Pattern in Java

 class NumberPattern
{
public static void main(String[] args) 
{
for (int i = 1; i <= 5; i++) 
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}             
System.out.println();
}
}
}

Output

 1
10
101
1010
10101

Java Program to Print Number Pattern

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

Output

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

Java Program to Print Number Pattern

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

Output

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

Java Program to Print Number Pattern

class NumberPattern
{
public static void main(String[] args) 
{
for (int i = 1; i <= 6; i++) 
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
             
System.out.println();
}
}
} 

Output

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

Prev Tutorial Next Tutorial

Advertisements
Download Projects