plz help
those sp are 4 space
I have to give those space
I have to submit this program tomorrow
plz help

Hey guys I have done
but some problem is still there
extra line feed is coming plz solve it

class pattern5
{
 public static void main()
 {
     int i,j,k,l,m,n,o;
     for(i=1;i<=5;i++)
     {
         System.out.print("*");
        }
        System.out.println();
       for(j=1;j<=3;j++)
       {
          for(l=j;l<=2;l++)
          {
              System.out.print("*");
            }

         for(k=4-j;k<=4;k=k+2)
         {
           System.out.print(" ");
           if(k>3)
           {
              System.out.print(" ");  
            }
        }

        for(m=j;m<=2;m++)
        {
            System.out.print("*");
        }  
            System.out.println();
        }
        for(int a=1;a<=2;a++)
        {
        if(a==1){
             System.out.print("** ");
            }

        if(a==2)
         {
             System.out.print("**");
            }
    }
    System.out.println();
    for(i=1;i<=5;i++)
     {
         System.out.print("*");
        }
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Like I said, you need to put your code in code tags too. This is the only time I'm going to do it for you.

class pattern5
{
 public static void main()
 {
     int i,j,k,l,m,n,o;
     for(i=1;i<=5;i++)
     {
         System.out.print("*");
        }
        System.out.println();
       for(j=1;j<=3;j++)
       {
          for(l=j;l<=2;l++)
          {
              System.out.print("*");
            }
           
         for(k=4-j;k<=4;k=k+2)
         {
           System.out.print(" ");
           if(k>3)
           {
              System.out.print(" ");  
            }
        }
    
        for(m=j;m<=2;m++)
        {
            System.out.print("*");
        }  
            System.out.println();
        }
        for(int a=1;a<=2;a++)
        {
        if(a==1){
             System.out.print("** ");
            }
        
        if(a==2)
         {
             System.out.print("**");
            }
    }
    System.out.println();
    for(i=1;i<=5;i++)
     {
         System.out.print("*");
        }
    }
}

You need to use some consistent indentation. It's too hard to read otherwise. The whole point of these exercises is to stick some nested looping and do some mathematical coding to figure out the pattern so that if your diamond changes to, say, ten or eleven wide, you have to change hardly anything. I think you need to start from scratch, as you have far too much hard coded. Stuff like this isn't what your after. I won't say it's "cheating", but... it's not what the goal is.

for(int a=1;a<=2;a++)
        {
        if(a==1){
             System.out.print("** ");
            }
        
        if(a==2)
         {
             System.out.print("**");
            }
    }

Here should be your general approach. If you see yourself writing an "if" statement, you're probably going about it incorrectly. You do not need any if statements. Just for-loops.

  1. Have a variable that represents the width of the diamond at its widest.
  2. Display a line of asterisks, using a for-loop based on 1.
  3. Set up an outer loop with a loop control variable called i. The loop parameters will relate to the value in 1. Steps 4 through 7 are inside this outer loop.
  4. Set up an inner loop within the for-loop in 3 with a loop control variable called j. The loop parameters will relate to the value of i. Display the left asterisks.
  5. Set up an inner loop within the for-loop in 3 with a loop control variable called j. The loop parameters will relate to the value of i. Display the spaces.
  6. Set up an inner loop within the for-loop in 3 with a loop control variable called j. The loop parameters will relate to the value of i. Display the right asterisks.
  7. Display a line return.
  8. Display a line of asterisks, using a for-loop based on 1.
This article has been dead for over six months. Start a new discussion instead.