Pyramid pattern programs in Java
In this post, you will learn how to print various pyramid pattern programs using the Java programming language. Such a type of logical question is generally asked in an interview or in competitive exams. This can be helpful to improve your logical programming skills and is helpful in game development and other logical application development.
A pattern program consists of patterns of numbers, letters, and symbols in a particular form. In the programming language, the loop condition is considered important for printing patterns. Generally, nested for loops are used in a number of patterns and shapes in printing programs.
Java pyramid pattern program 1
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 8;
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output of the above code
*
**
***
****
*****
******
*******
********
Java pyramid pattern program 2
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
// Print i number of stars
for (int i=1; i<=rows; i++)
{
// Print space in decreasing order
for (int j=rows; j>i; j--)
{
System.out.print(" ");
}
// Print star in increasing order
for (int k=1; k<=i; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output of the above code:
*
**
***
****
*****
******
*******
Java pyramid pattern program 3
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
for (int i=1; i<=rows; i++)
{
// Print space in decreasing order
for (int j=rows; j>i; j--)
{
System.out.print(" ");
}
// Print star in increasing order
for (int k=1; k<=(i * 2) -1; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output of the above code:
*
***
*****
*******
*********
***********
*************
Java pyramid pattern program 4
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
for (int i=rows; i>=1; i--)
{
// Print star in decreasing order
for (int k=1; k<=(i * 2) -1; k++)
{
System.out.print("*");
}
System.out.println();
// Print space in increasing order
for (int j=rows; j>=i; j--)
{
System.out.print(" ");
}
}
}
}
Output of the above code:
*************
***********
*********
*******
*****
***
*
Java pyramid pattern program 5
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();
}
}
}
Output of the above code:
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
Java pyramid pattern program 6
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();
}
}
}
Output of the above code
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
Java pyramid pattern program 7
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();
}
}
}
Output of the above code
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
Java pyramid pattern program 8
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();
}
}
}
Output of the above code
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
Java pyramid pattern program 9
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();
}
}
}
Output of the above code
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
Java pyramid pattern program 10
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();
}
}
}
Output of the above code
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
Java pyramid pattern program 11
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();
}
}
}
Output of the above code
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
Java pyramid pattern program 12
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();
}
}
}
Output of the above code
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
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity 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