Star Pattern Programs In java
- Categories Nested loops, Java, Iterative Statements
Watch our video on How to solve any Star Pattern Program in java- Click Here
1. Write a python program to print following star pattern:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Code:
public class IncreasingTriangle
{
public static void main (String args[])
{
int n = 5;
for (int i = 1; i<=n ; i++)
{
for (int j = 1; j<=n; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
2. Write a python program to print following pattern:
*
* *
* * *
* * * *
* * * * *
Code:
public class IncreasingTriangle
{
public static void main (String args[])
{
int n = 5;
for (int i = 1; i<=n ; i++)
{
for (int j = 1; j<=i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
3. Write a python code to print following star pattern:
* * * * *
* * * *
* * *
* *
*
Code:
public class DecreasingTriangle
{
public static void main (String args[])
{
int n = 5;
for (int i = 1; i<=n ; i++) {
for (int j = i; j<=n; j++) {
System.out.print(“* ”);
}
System.out.println();
}
}
}
4. Write a python code to print following pattern:
*
* *
* * *
* * * *
* * * * *
Code:
import java.util.Scanner;
public class RightStarTriangle
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows:");
int n= sc.nextInt();
for (int i = 1; i<=n ; i++) {
for (int j = i; j<=n; j++) {
System.out.print(" ");
}
for (int j = 1; j<=i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
5. Write a python program to print following star pattern:
* * * * *
* * * *
* * *
* *
*
Code:
public class RightStarTriangle
{
public static void main(String args[])
{
int n= 5;
for (int i = 1; i<=n ; i++) {
for (int j = 1; j<=i; j++) {
System.out.print(“ ”);
}
for (int j = i; j<=n; j++) {
System.out.print(“* ”);
}
System.out.println();
}
}
}
6. Write a python program to print following hill star pattern:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Code:
import java.util.Scanner;
public class StarPyramid
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter Number of Rows : ");
int n = scan.nextInt();
for (int i = 1; i<=n ; i++) {
for (int j = i; j<=n; j++) {
System.out.print(" ");
}
for (int j = 1; j< i; j++) {
System.out.print("* ");
}
for (int j = 1; j<=i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
7. Write a python program to print revers hill star pattern:
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Code:
public class StarRe Hill
{
public static void main(String args[])
{
int n = 5;
for (int i = 1; i<=n ; i++) {
for (int j = 1; j<=i; j++) { // increasing
System.out.print(“ ”);
}
for (int j = i; j< n; j++) { // decreasing
System.out.print(“* ”);
}
for (int j = i; j<=n; j++) { // decreasing
System.out.print(“* ”);
}
System.out.println();
}
}
}
8. Write a python code to print Dimond pattern with star:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Code:
import java.util.Scanner;
public class StarPyramid
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter Number of Rows : ");
int n = scan.nextInt();
for (int i = 1; i<=n;i++){
for (int j = i; j<=n; j++) {
System.out.print(" "); // decreasing
}
for (int j = 1; j< i; j++) { // increasing
System.out.print("* ");
}
for (int j = 1; j<=i; j++) { // increasing
System.out.print("* ");
}
System.out.println();
}
for (int i = 1; i<=n ; i++) {
for (int j = 1; j<=i; j++) { // increasing
System.out.print(" ");
}
for (int j = i; j< n; j++) { // decreasing
System.out.print("* ");
}
for (int j = i; j<=n; j++) { // decreasing
System.out.print("* ");
}
System.out.println();
}
}
}
9. Write a python code to print Butterfly pattern with star:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * *
* * * * *
* *
Code:
import java.util.Scanner; public class StarPattern4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println(" Printing the pattern "); for (int i=1; i<=rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int k = i*2; k <= rows*2-1; k++) { System.out.print(" "); } for (int l = 1; l <= i; l++) { System.out.print("*"); } System.out.println(); } for (int i=1; i<=rows-1; i++) { for (int j = rows-1; j >= i; j--) { System.out.print("*"); } for (int k = 1; k <= i*2; k++) { System.out.print(" "); } for (int l = rows-1; l >= i; l--) { System.out.print("*"); } System.out.println(); } scanner.close(); } }