Click here to Skip to main content
15,885,101 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need it to be like this:


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


but I cannot find out how to decrement every line by 2 stars so it comes the same length as first line

What I have tried:

So basically this is my code:

Java
for (int i = 0; i<=5; i++){
         
          if (i == 1 || i == 3){
              System.out.print(" ");
              System.out.print(" ");
              
            }
            if (i == 2){
           System.out.print(" ");
           System.out.print(" ");   
           System.out.print(" ");
           System.out.print(" ");
           
        }
        
         for (int j = 0; j <=12; j++){  
               System.out.print("*");
        }
             System.out.println();
    }
    
    
}   


}
Posted
Updated 25-Nov-18 10:28am
v2
Comments
Mohibur Rashid 25-Nov-18 16:18pm    
What would happen if line number goes to 7 or 8 or 9?

1 solution

Java
final int ROWS = 2;
final int COLS = 12;

for ( int row = - ROWS; row <= ROWS; ++row)
{
  for (int col = 1; col <= COLS; ++col)
  {
    int limit = row >= 0 ? ROWS - row : ROWS + row;
    limit *= 2;
    char c = col <= limit ? ' ' : '*';
    System.out.print(c);
  }
  System.out.println();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900