Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i want to do this shape :

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

code :

public void displayPyramid(int x, int y)
{
  row = x;
  col = y;
  int r =0;
  int c =0;
  
  while(r<row)>
   {
      while(c<col)>
       { 
        System.out.println("* ");
        c++;
       }
       System.out.println();
       r++;
   }
}
Posted
Updated 1-May-11 23:26pm
v2
Comments
Mohammed Ahmed Gouda 2-May-11 5:24am    
* * * * *
* * * *
* * *
* *
*
walterhevedeich 2-May-11 5:26am    
a homework?
Mohammed Ahmed Gouda 2-May-11 7:17am    
no i am just trying to improve myself

Algo Steps:
1. Start loop from 5 to 1 step -1
2. Show the stars as per the current loop number
3. Put a line break when the loop is reduced by 1

Done! Try!
 
Share this answer
 
Comments
Mohammed Ahmed Gouda 2-May-11 7:47am    
i dont understand
Sandeep Mewara 2-May-11 9:17am    
What was confusing?
Sergey Alexandrovich Kryukov 2-May-11 8:21am    
Cognition exercise detected! My 5.
--SA
Sandeep Mewara 2-May-11 9:17am    
Thanks SA.
Look at your diagram and count the number of spaces at the beginning, and total number of stars on each line. That should give you a reasonable clue as to the loop counters you need for each row.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-May-11 8:21am    
As the OPs purpose was to improve himself, this should be enough :-), my 5.
--SA
You can try this, may it help you
for (int i = 5; i > 0; i--)
{
    int y = 5;
    y = y - i;
    for (int e = y; e > 0; e--)
    {
        Console.Write(" ");
    }
    for (int j = i; j > 0; j--)
    {
        Console.Write("* ");
    }
    Console.Write("\n");
}


Eshaq
 
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