Plz help to print


*******
**1sp**
* 5 sp *
**1sp**
*******

Recommended Answers

All 8 Replies

System.out.println()

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

plz help me to print that pattern

Not that I do not want help but here is a rule which state We only give homework help to those who show effort, in the meaning show us what you tried so far and we will direct you. Do not expect people to do your homework, that way you never learn.

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("*");
        }
    }
}

I explained here that you need to to post the pattern in code tags or else all the spacing gets stripped out, as well as how to do it.

http://www.daniweb.com/forums/thread206645.html

You also need to post your code in code tags.

[code=JAVA] // paste code here

[/code]

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

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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.