Star pattern in java – Java Program to Print K Shape Star Pattern

Program to Print K Shape Star Pattern

Star pattern in java: In this article we are going to see how to print the k-shape star program.

Example-1

When row value=4
****
***
**
*
**
***
****
Example-2:

When row value=5
*****
****
***
**
*
**
***
****
*****

Now, let’s see program to print it.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer for loop to print all rows.
    • First  inner for loop to print column values i.e., first inner for loop will print all the stars in the column(in decreasing order).
  • Take second outer for loop to print all rows.
    • First  inner for loop to print column values i.e., first inner for loop will print all the stars in the column(in increasing order).

JAVA Code:

Method-1 : Static Star Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,d;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //printing row value 
    for (r = row ; r >= 1; r--)
        {
            for (c = r; c >= 1; c--)
                System.out.print("*");
            System.out.println();
        }
        for (r = 2; r <= row; r++)
        {
            for (c= r; c >= 1; c--)
                System.out.print("*");
            System.out.println();
        }
    }
}
Output:

Enter Rows :5
*****
****
***
**
*
**
***
****
*****

Method-2 : User Input Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,d;
    char f;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // entering random character
    System.out.print("Enter character : ");
    f = s.next().charAt(0);
    //printing row value 
    for (r = row ; r >= 1; r--)
        {
            for (c = r; c >= 1; c--)
                System.out.print(f);
            System.out.println();
        }
        for (r = 2; r <= row; r++)
        {
            for (c= r; c >= 1; c--)
                System.out.print(f);
            System.out.println();
        }
    }
}
Output:

Enter Rows : 5

Enter character : @
@@@@@
@@@@
@@@
@@
@
@@
@@@
@@@@
@@@@@

Explanation :

First outer for loop :

Iteration-1

r=5 (row) (passes the first for loop condition) as it will iterate up to r>=1 times.

then first inner for loop will print the “* ”   r to 1 , that means 5 time .

So, star will be printed five time.

*****

Iteration-2

r=4 (row) (passes the first for loop condition) as it will iterate up to r>=1 times.

then first inner for loop will print the “* ”   r to 1 , that means 4 time .

So, star will be printed five time.

****

Iteration-3

r=3 (row) (passes the first for loop condition) as it will iterate up to r>=1 times.

then first inner for loop will print the “* ”   r to 1 , that means 3 time .

So, star will be printed five time.

***

Iteration-4

r=2 (row) (passes the first for loop condition) as it will iterate up to r>=1 times.

then first inner for loop will print the “* ”   r to 1 , that means 2 time .

So, star will be printed five time.

**

Iteration-5

r=1 (row) (passes the first for loop condition) as it will iterate up to r>=1 times.

then first inner for loop will print the “* ”   r to 1 , that means 1 time .

So, star will be printed five time.

*

Now when r=0, first for loop condition will fail so no other loops will be executed.

Second outer for loop :

Iteration-1

r=2 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

then first inner for loop will print the “* ”   r to 1 , that means 2 time .

So, star will be printed five time.

**

Iteration-2

r=3 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

then first inner for loop will print the “* ”   r to 1 , that means 3  time .

So, star will be printed five time.

***

Iteration-3

r=4 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

then first inner for loop will print the “* ”   r to 1 , that means 4  time .

So, star will be printed five time.

****

Iteration-4

r=5 (row) (passes the first for loop condition) as it will iterate up to r<=row times.

then first inner for loop will print the “* ”   r to 1 , that means 4  time .

So, star will be printed five time.

*****

Now when r=6,second outer  for loop condition will fail so no other loops will be executed.

At last , we will see a pattern like this ,

*****
****
***
**
*
**
***
****
*****

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
    for (r = row ; r >= 1; r--)
        {
            for (c = r; c >= 1; c--)
                printf("*");
            printf("\n");
        }

        for (r = 2; r <= row; r++)
        {
            for (c= r; c >= 1; c--)
                printf("*");
            printf("\n");
        }
   return 0;
}

Output:

Enter Rows :5

*****
****
***
**
*
**
***
****
*****

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
     for (r = row ; r >= 1; r--)
        {
            for (c = r; c >= 1; c--)
                cout << "*";
            cout << "\n";
        }

        for (r = 2; r <= row; r++)
        {
            for (c= r; c >= 1; c--)
                cout << "*";
            cout << "\n";
        }
    return 0;
    
}
Output:

Enter Rows :5
*****
****
***
**
*
**
***
****
*****

Related Java Star Pattern Programs: