Question

How can I make this pattern in Java? 11 11 11 11 1111 11 11 11...

How can I make this pattern in Java?

11 11
11 11
1111
11 11
11 11

Thanks for your help

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

public class Main
{
   public static void main(String[] args) {
        System.out.println("printing the required pattern");//if you dont want to see this line put // at the start of line
        int n=5;//you can change this n to observe changes in your pattern
        int index;
        for(int i=0;i<n;i++){
            if(i!=(n-1)/2){//if the row is not a middle row
                index=(n-1)/2;//this stroes index of position to filled with empty space
                for(int j=0;j<index;j++){
                    System.out.print("1");//print 1
                }
                System.out.print(" ");//print space
                 for(int j=index+1;j<n;j++){
                    System.out.print("1");//print 1 in remaining positions
                }
                System.out.println("");//to move to next line
            }//end of if
            else{//if the row is middle row
                 for(int j=0;j<n-1;j++){
                    System.out.print("1");//print 1 in n-1 places
                }
                System.out.println(" ");//print a empty space and move to next line println prints next string to next line
              
            }//end of else
          
        }//end of for
   }//end of main function
}//end of class

Output:

printing the required pattern 11 11 11 11 1111 11 11 11 11 ... Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
How can I make this pattern in Java? 11 11 11 11 1111 11 11 11...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT