Table of Contents
To begin with, Pattern programs in Java for printing numbers is one of the easiest ways to embark on your Java coding skills journey. It is of utmost importance to have knowledge of these pattern programmes in Java as they play a vital role when you appear for an interview as a fresher. As in, you are asked various questions about it. That is to say, having knowledge of these pattern programmes in Java is an advantage and will help you appear for an interview and take your career to new heights.
In this article, I have taken 20 pattern programmes in Java for Printing numbers and have also tried to solve them. So, let’s have a look at each one of them and understand how to use them.
Here are the Top 20 Pattern Programs in Java For Printing Numbers. Have a glance at them and understand them.
1. 1st Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
//it is a simple sentence which prints arguments delivered to it
System.out.println; ()
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
//Next step is to print upper half of the pattern
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = rows-1; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
2. 2nd Pattern – Number Pattern Programs in Java
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
3. 3rd Pattern – Number Pattern Programs in Java
7 6 5 4 3 2 1
7 6 5 4 3 2
7 6 5 4 3
7 6 5 4
7 6 5
7 6
7
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
4. 4th Pattern – Number Pattern Programs in Java
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
5. 5th Pattern
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
6. 6th Pattern – Number Pattern Programs in Java
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
7. 7th Pattern – Number Pattern Programs in Java
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
1 2 3 4 5 6 7 6 5 4 3 2 1
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
//Printing first half of the row
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
//Printing second half of the row
for (int j = i-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
8. 8th Pattern
7
7 6
7 6 5
7 6 5 4
7 6 5 4 3
7 6 5 4 3 2
7 6 5 4 3 2 1
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
9. 9th Pattern – Number Pattern Programs in Java
1234567
234567
34567
4567
567
67
7
67
567
4567
34567
234567
1234567
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking value of the rows from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println ("Printing the pattern....!!!");
//Printing upper half of the pattern
for (int i = 1; i <= rows; i++)
{
//Printing i spaces at the starting of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to value of the rows at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j);
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = rows-1; i >= 1; i--)
{
//Printing i spaces at the starting of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to value of the rows at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j);
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
10. 10th Pattern
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking value of the rows from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println("Printing the pattern....!!!");
//Printing top half of the pattern
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Printing bottom half of the pattern
for (int i = 2; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
11. 11th Pattern – Number Pattern Programs in Java
1
10
101
1010
10101
101010
1010101
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}
System.out.println();
}
sc.close();
}
}
12. 12th Pattern – Number Pattern Programs in Java
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass ()
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking value of the rows from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt(); ()
System.out.println("Printing thepattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
13. 13th Pattern – Number Pattern Programs in Java
1111111
1111122
1111333
1114444
1155555
1666666
7777777
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows-i; j++)
{
System.out.print(1);
}
for (int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
sc.close();
}
}
14. 14th Pattern
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7
6 7
5 6 7
4 5 6 7
3 4 5 6 7
2 3 4 5 6 7
1 2 3 4 5 6 7
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking value of the rows from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println("Printing the pattern....!!!");
//Printing upper half of the pattern
for (int i = 1; i <= rows; i++)
{
//Printing i spaces at the starting of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to value of the rows at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = rows-1; i >= 1; i--)
{
//Printing i spaces at the starting of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
15. 15th Pattern – Number Pattern Programs in Java
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in); ()
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt(); ()
System.out.println("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
int num = i;
for (int j = 1; j <= i; j++)
{
System.out.print(num+" ");
num = num+rows-j;
}
System.out.println();
}
sc.close();
}
}
16. 16th Pattern – Number Pattern Programs in Java
1010101
0101010
1010101
0101010
1010101
0101010
1010101
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println("Printing the pattern....!!!");
for (int i = 1; i <= rows; i++)
{
int num;
if(i%2 == 0)
{
num = 0;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
else
{
num = 1;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
System.out.println();
}
sc.close();
}
}
17. 17th Pattern
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
System.out.println;
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//This tells us that it scans the next token of the input
int noOfRows = sc.nextInt();
int value = 1;
System.out.println("Printing the pattern :");
for (int i = 1; i <= noOfRows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(value+"\t");
value++;
}
System.out.println();
}
}
}
18. 18th Pattern – Number Pattern Programs in Java
1 2 3 4 5 6 7
2 3 4 5 6 7 1
3 4 5 6 7 1 2
4 5 6 7 1 2 3
5 6 7 1 2 3 4
6 7 1 2 3 4 5
7 1 2 3 4 5 6
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println("Print your pattern....!!!");
for(int i=1;i< rows+1 ;i++)
{
for(int j=i; j < rows+1 ;j++)
{
System.out.print(j + " ");
}
for(int k=1; k < i ;k++)
{
System.out.print(k + " ");
}
System.out.println();
}
sc.close();
}
}
19. 19th Pattern – Number Pattern Programs in Java
1
2 13
3 12 14
4 11 15 22
5 10 16 21 23
6 9 17 20 24 27
7 8 18 19 25 26 28
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println;
//This tells us that it scans the next token of the input
int rows = sc.nextInt();
System.out.println("Printing the pattern....!!!");
for(int i=1 ; i <= rows ; i++)
{
System.out.print(i + " ");
int n = i;
for(int j = 1; j < i ; j++)
{
if(j%2 != 0)
{
System.out.print((n + ((2 * (rows + 1 - i)) - 1)) + " ");
n = n + ((2 * (rows + 1 - i)) - 1);
}
else
{
System.out.print((n + 2 * (i - j)) + " ");
n = n + 2 * (i - j);
}
}
System.out.println();
}
//Close the resources
sc.close();
}
}
20. 20th Pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
//Scanner class in java.util package is used to get the input of the primitive data types
import java.util.Scanner;
//It is added so that everyone globally can access the method
public class MainClass
{
//anyone can call this method even if you’re outside the class you are right now in
public static void main(String[] args)
{
System.out.println;
//It reads input from the keyboard
Scanner sc = new Scanner(System.in);
//This tells us that it scans the next token of the input
int noOfRows = sc.nextInt();
for (int i = 1; i <= noOfRows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Conclusion
On the positive side, this article brings to you a fascinating world of 20 Patterns in Java for Printing Numbers. Simultaneously, it will help you in appearing for an interview too and it’ll help you start your coding career.
To learn more about Java and the course, click below:
Other Recommended Courses
Henry Harvin Education offers Java courses that you can enrol for and upskill yourself in Java Pattern Programmes.
- JAVA Foundation with DS & Algo Combo Certification Course
- Java Programming Course for Beginners
- Certified Java Full Stack Developer Course
You may contact them at +91 9891953953 (WhatsApp number)
FAQs
Java is the most portable and powerful programming language. It is used with Python and HTML to make web pages even more interesting. In today’s technology, having a vast knowledge of these softwares gives you a wide array of global opportunities.
Patterns help us to know looping structures in general purpose programming language. It is asked in job interviews and campus placements.
The other type of Java patterns are:
Star Patterns, Number Patterns, Character Patterns