Number Pattern- Odd Number with Pyramid

Number Pattern- Odd Number with Pyramid

150 150 mrrohit

To print any patterns of numbers or stars (*) in Java Programming, you have to use two loops.

The first is an outer loop and the second is an inner loop.

The outer loop is responsible for rows and the inner loop is responsible for columns.

logic
 
// outer for loop
for(int i=0; i<5; i++)    
         {
//inner for loop will be write here
}
code
 
public class StarPattern
{
    public static void main(String[] args) {
        int rows=4;
        int numberCount=rows;
         for (int i=rows; i>=1; i--)
        {
            // Print star in decreasing order
             for (int j=rows; j>i; j--)
            {
                System.out.print("  ");
            }
            for (int k=1; k<=(i * 2) -1; k++) 
            { 
                System.out.print(numberCount+" "); 
            } 
            System.out.println(); 
            // Print space in increasing order 
           
            numberCount--;
            
        }
        numberCount++;
        //System.out.println(); 
         for (int i=2; i<=rows; i++) 
        { 
            numberCount++;
            // 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(numberCount+" ");
            }
            System.out.println();
        }
    }
}

 

 

Output:
star pattern

 

 

 

Press Ctrl+C to copy the following code.
"