Number pattern programs in Java
In this post, you will learn how to print various number pattern programs using Java programming. Such type of logical question is generally asked in an interview or in competitive exams. This can be helpful to improve your logical programming skill and helpful in game development and other logical application development.
A pattern program consists of patterns of numbers, letters, symbols in a particular form. In the programming language, the loop condition is considered important for printing patterns. Generally, nested for loops is used in number of patterns and shapes printing programs.
Java number pattern program 1
Suppose someone asks you to print the following number pattern using Java.
Number 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 7 8
The solution is as follows.
public class MainClass
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 8;
System.out.println("Number pattern : ");
for (int x = 1; x <= rows; x++)
{
for (int y = 1; y <= x; y++)
{
System.out.print(y+" ");
}
System.out.println();
}
}
}
Java number pattern program 2
Suppose we have to print repeated number patterns of up to eight rows as shown below.
Number 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
8 8 8 8 8 8 8 8
The Java programming solution of the above pattern is as follows. This number pattern program uses the nested for loops.
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 8;
System.out.println("Number pattern : ");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Java number pattern program 3
Suppose someone asks you to print the following number pattern using 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
1 2 3 4 5 6 7 8
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
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 8;
//Printing upper half of the pattern
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
// Printing space
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+" ");
}
// Printing space
System.out.println();
}
}
}
Java number pattern program 4
Suppose someone asks you to print the following reverse triangle shape number pattern program using Java.
1 2 3 4 5 6 7 8
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
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 8;
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
Java number pattern program 5
Suppose someone asks you to print the following k shape number pattern program using Java.
1 2 3 4 5 6 7 8
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
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 7 8
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 8;
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Java number pattern program 6
Suppose someone asks you to print the following triangle shape number pattern program using 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
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 7;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}
Java number pattern program 7
Suppose someone asks you to print the following triangle shape number pattern program using Java.
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
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 7;
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Java number pattern program 8
Suppose someone asks you to print the following triangle shape number pattern program using Java.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 7;
int k = 1;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}
Java number pattern program 9
Suppose someone asks you to print the following number pattern program using 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
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10
11 11 11 11 11 11 11
12 12 12 12 12 12
13 13 13 13 13
14 14 14 14
15 15 15
16 16
17
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 9;
int temp = 1;
for (int i = 1; i <= rows; i++)
{
for (int k = 1; k <= i; k++)
{
System.out.print(temp + " ");
}
temp++;
System.out.println();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int k = i; k >= 1; k--)
{
System.out.print(temp + " ");
}
temp++;
System.out.println();
}
}
}
Java number pattern program 10
Suppose someone asks you to print the following triangle shape number pattern program using 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
8 7 6 5 4 3 2 1
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 8;
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Java number pattern program 11
Suppose someone asks you to print the following triangle shape number pattern program using Java.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 8;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
int temp = 1;
for (int k = 1; k <= i; k++)
{
System.out.print(temp + " ");
temp = temp * (i - k) / (k);
}
System.out.println();
}
}
}
Java number pattern program 12
Suppose someone asks you to print the following reverse triangle shape number pattern program using Java.
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
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
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 9;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= rows - i + 1; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}
Java number pattern program 13
Suppose someone asks you to print the following square shape number pattern program using Java.
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 9;
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();
}
}
}
Java number pattern program 14
Suppose someone asks you to print the following triangle shape number pattern program using Java.
1
2 10
3 11 18
4 12 19 25
5 13 20 26 31
6 14 21 27 32 36
7 15 22 28 33 37 40
8 16 23 29 34 38 41 43
9 17 24 30 35 39 42 44 45
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 9;
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();
}
}
}
Java number pattern program 15
Suppose someone asks you to print the following triangle shape number pattern program using 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
The solution is as follows -
public class NumberPatternProgram
{
public static void main(String[] args)
{
// defining pattern rows
int rows = 7;
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();
}
}
}
Related Articles
Matrix multiplication in JavaElectricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number