print a pattern java l

i want to print the pattern in java and i have tried the following code. please guide me to solve my mistake.

import java.util.*;

class sp10
{
    public static void main(String args[])
    { 
        int i,j,n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter value of n");
        n = sc.nextInt();

        for(i=n;i>=1;i--)
        {
            for(j=i;j<n;j++)
            {
                System.out.print(" ");
            }

            for(j=1;j<=(2*i-1);j++)
            {
                if(j==1 || j==(2*i-1) ||i==n)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println("");
        } 

        for(i=2;i<=n;i++)
        {
            for(j=i;j<n;j++)
            {
                System.out.print(" ");
            }
            for(j=1;j<=(2*i-1);j++)
            {
                if(j==1 || j==(2*i-1)|| i==n)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println("");
        } 
    }
}

i am trying it java can you please guide me. it should print 5 star in first and last line. the below mentioned is the exact pattern

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

Well, let's look at the pattern.

n = 5
*****
*   *
 * *
  *
 * *
*   *
***** 

The first line and the last line is just print * n times.

private static void printTop(int n) {
    System.out.println("*".repeat(n));
}

private static void printBottom(int n) {
    printTop(n);
}

Though I wrote two methods here, you can just define one method like printStar(int n) and reuse it on the first and last line.

The real problem is to implement the middle part. Well, this still can be implemented in a relatively simple way. You just need to move * from column 0 to right and from n-1 to left as the row increased.

private static void printMiddle(int n) {
    for (int i = 0; i < n; i++) {
        int p = i;
        for (int j = 0; j < n; j++) {
            if (j == p || j == n - 1 - p) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

p is the offset from column 0 to right or column n-1 to left.

Now, you have everything. You can write the main method like the following:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter value of n");
    int n = sc.nextInt();

    printTop(n);
    printMiddle(n);
    printBottom(n);
}

Top 30 Patterns in Java: How to Print Star, Number and Character , This article precisely focuses on pattern programs in Java. You will learn System.out.print("Enter the number of rows: "); System.out.print(l); The course is for beginners in java or any other object oriented language. In this course various kind of patterns are discussed from beginner to intermediate level. The students will gain control over the looping statements and understand the value of loops to print various pattern in quick and easy way.

I suggest the following solution:

import java.util.*;

public class sp10 {

    public static void main(String args[]) {
        int i, j, n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter value of n");
        n = sc.nextInt();

        System.out.print(" ");
        for (i = 0; i < 2*n - 3; i++) {
            System.out.print("*");
        }
        System.out.println("");
        for (i = n-1; i >= 1; i--) {
            for (j = i; j <n; j++) {
                System.out.print(" ");
            }

            for (j = 1; j <= (2 * i - 1); j++) {
                if (j==1 || j == (2 * i - 1) || i == n ) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
        for (i = 2; i <= n-1; i++) {
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
            for (j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1) || i == n) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
        System.out.print(" ");
        for (i = 0; i < 2*n - 3; i++) {
            System.out.print("*");
        }
    }
}

I hope is what you're looking for, because you question was a little bit difficult to understand...

With such solution the output is (for n = 4):

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

How to Print Pattern in Java, How to Print Pattern in Java with java tutorial, features, history, variables, for ( int l = 2; l <= i; l++); {; System.out.print(l);; }; System.out.println();; }; for (int i = 3;� Pattern Programs in Java. Star Patterns in Java; Numeric Patterns; Character Patterns; Let’s get started. :-) Star Patterns in Java. First, let us begin with the basic and the commonly asked pattern program in Java i.e Pyramid. 1. Pyramid Program * * * * * * * * * * * * * * * Let’s write the java code to understand this pattern better.

Probably, what you intended to do is as follows:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        int i, j, n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter value of n: ");
        n = sc.nextInt();

        for (i = n; i >= 1; i--) {
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }

            for (j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1) || (i == n && j % 2 == 1))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println("");
        }
        for (i = 2; i <= n; i++) {
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
            for (j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1) || (i == n && j % 2 == 1))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println("");
        }
    }
}

A sample run:

Enter value of n: 4
* * * *
 *   *
  * *
   *
  * *
 *   *
* * * *

However, if you insist that you need to print exactly what you have shown in the question, you can do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        int i, j, n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter value of n: ");
        n = sc.nextInt();

        for (i = n; i >= 1; i--) {
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }

            for (j = 1; j <= (2 * i - 1); j++) {
                if ((j == 1 && i != n) || (j == (2 * i - 1) && i != n) || (j != 1 && i == n && j != (2 * i - 1)))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println("");
        }
        for (i = 2; i <= n; i++) {
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
            for (j = 1; j <= (2 * i - 1); j++) {
                if ((j == 1 && i != n) || (j == (2 * i - 1) && i != n) || (j != 1 && i == n && j != (2 * i - 1)))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println("");
        }
    }
}

Output:

Enter value of n: 4
 ***** 
 *   *
  * *
   *
  * *
 *   *
 ***** 

30 Pattern Programs in Java: Star, Number & Character Patterns , Top 30 Patterns in Java: How to Print Star, Number and Character for ( int l = 2 ; l <= i; l++) { System.out.print(l); } System.out.println(); } for ( int� In this video we will learn how to print alphabet i pattern using java programming language.

How to Print Pyramid Pattern in Java? Program Example, You need to write a Java program to print above pyramid pattern. How many for (int j = 1; j <=n; j++) { System.out.print(l+4-j); } l=l+4; } System.out.println(""); } Write a Java Program to Print Square Star Pattern using For Loop, and While Loop with example. Java Program to Print Square Star Pattern using For Loop This program allows the user to enter any side of a square (integer value). Next, this Java program displays the square star pattern until it reaches the user-specified rows and columns.

Java program to print L-shape pattern, /*For L-shape pattern, we need print white space from 1st row after every column up to the second last row.*/ if(j==1 || (i==9 && j!=0 && j<9)) { Java Program to Print Number Pattern. In Java language you can easily print number pattern using for loop and also using while loop, here i will show you in simple way to print these all patterns.

19 Different Number Pattern Programs in Java, Java programs to print the numbers or any different pattern is one of the easiest ways to kick off your coding skills in java. In this post I have taken some different� A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Comments
  • ***** * * * * * * * * * ***** this is the exact pattern which i need
  • Please format your code properly.
  • what consequence do you want your input n to have?
  • edureka.co/blog/30-pattern-programs-in-java -example number 11, 13, 14
  • what should be the input?
  • thanks everyone for guiding me and solving my query
  • @AbhishekDutta - Do not forget to accept the answer so that future visitors can also use the solution confidently. Check meta.stackexchange.com/questions/5234/… to learn how to do it. Feel free to comment in case of any doubt/issue.