➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
Pyramid Program in Java | There are many pattern programs are written by programmers for practice purposes. The pyramid star pattern in Java is one of them. In this post, we will display half pyramid star pattern, inverted half pyramid star pattern, full pyramid star pattern, full pyramid star pattern at the center of the screen, and inverted full pyramid star pattern.
Half Pyramid Star Pattern
1. Write a Java program to display the half pyramid star pattern.
*
* *
* * *
* * * *
* * * * *
The Java program for the above half pyramid star pattern is,
public class Pyramid {
public static void main(String[] args) {
// outer loop for row
for (int i=1; i <= 5; i++) {
// inner loop for column
for(int j=1; j <= i; j++) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
}
}
The previous program uses the increment operator to display the Half pyramid star pattern in Java. But we can also use the decrement operator.
Half pyramid star pattern using both increment and decrement operators,
Here we will use decrement operator for outer loop and increment operator for the inner loop to print above print.
for (int i=5; i >= 1; i--) {
for(int j=i; j <= 5; j++) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
Now, we will use the increment operator for the outer loop and decrement operator for the inner loop to print the same (above) pattern.
for (int i=1; i <= 5; i++) {
for(int j=i; j >= 1; j--) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
Inverted Half Pyramid Star Pattern
2. Write a Java program to display the inverted half pyramid pattern of star.
* * * * *
* * * *
* * *
* *
*
The code for the inverted half pyramid star pattern is,
public class InvertedPyramid {
public static void main(String[] args) {
for (int i=5; i >= 1; i--) {
for(int j=i; j >= 1; j--) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
}
}
3. Display the below triangle of star pattern in Java.
*
**
***
****
*****
The code for the above pattern is given below,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
// space
for(int j=1; j <= n-i; j++)
System.out.print(" ");
// star
for(int k=1; k <= i; k++)
System.out.print("*");
// new line
System.out.println();
}
}
}
4. Write a C program to display the below triangle of star pattern.
*****
****
***
**
*
The Java code for the above star pattern,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= n; j++)
if(j < i)
System.out.print(" "); // space
else
System.out.print("*"); // star
// new line
System.out.println();
}
}
}
Full Pyramid Star Pattern in Java
5. Write a program to display the full pyramid star pattern.
*
***
*****
*******
*********
Full pyramid star pattern program in Java can be written as,
import java.util.Scanner;
public class FullPyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
// space
for(int j=1; j <= n-i; j++)
System.out.print(" ");
// star
for(int k=1; k <= (2*i-1); k++)
System.out.print("*");
// new line
System.out.println();
}
}
}
There are many other ways to display the same pattern,
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= 2*n; j++)
if(j <= n-i)
System.out.print(" "); // space
else if(j < n+i)
System.out.print("*"); // star
// new line
System.out.println();
}
The same pattern also can be displayed using the recursion technique,
import java.util.Scanner;
public class FullPyramid {
// static variable
static int stars = -1;
// main method
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
displayFullPyramid(n);
}
private static void displayFullPyramid(int n) {
if(n <= 0) return;
// variables
int space = n-1;
stars += 2;
// space
for(int i = 0; i < space; i++)
System.out.print(" ");
// stars
for(int j = 0; j < stars; j++)
System.out.print("*");
// new line
System.out.println();
// recursive call
displayFullPyramid(--n);
}
}
6. Full pyramid star pattern at the center of the screen in Java,
Generally, on a computer screen, we can print Maximum 80 characters horizontally. Here we will print the full pyramid for n lines.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0, c=80;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= (c/2-i); j++)
System.out.print(" "); // space
for(int k=1; k <= (2*i-1); k++)
System.out.print("*"); // star
// new line
System.out.println();
}
}
}
Inverted Full Pyramid Star Pattern
7. Write a program to display inverted full pyramid star pattern
*********
*******
*****
***
*
Solution1:- Using only increment operators,
import java.util.Scanner;
public class InvertedPyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j < 2*n; j++)
if(j < i)
System.out.print(" "); // spaces
else if(j <= 2*n-i)
System.out.print("*"); // stars
// new line
System.out.println();
}
}
}
Solution2:- Using both increment and decrement operators,
for (int i=n; i >= 1; i--) {
for(int j=1; j <= n-i; j++)
System.out.print(" "); // spaces
for(int k=1; k <= 2*i-1; k++)
System.out.print("*"); // stars
// new line
System.out.println();
}
Solution3:-
for (int i=n; i >= 1; i--) {
for(int j=n; j > i; j--)
System.out.print(" "); // spaces
for(int k=1; k <= 2*i-1; k++)
System.out.print("*"); // stars
// new line
System.out.println();
}
8. Write a Java program to display the below triangle star pattern
*
* *
* * *
* * * *
* * * * *
Code for the above pattern is,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= n-i; j++)
System.out.print(" "); // spaces
for(int k=1; k <= i; k++)
System.out.print("* "); // stars
// new line
System.out.println();
}
}
}
9. Write a Java program to display the below triangle star pattern
* *
** **
*** ***
**** ****
**********
The code for the above pattern is,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= 2*n; j++)
if(j <= i || j > (2*n-i))
System.out.print("*"); // star
else
System.out.print(" "); // space
// new line
System.out.println();
}
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!