Javatpoint Logo
Javatpoint Logo

Fish Pattern in Java

In this section, we will understand how we can implement the logic of Fish Pattern. The fish pattern is one of the most complex patterns to implement code.

In order to implement the logic or code for Fish Pattern, we take input N from the user, and then we print it over 2N+1 rows.

Let's take some examples of fish patterns based on the user input N.

Example 1: N=3

Fish Pattern in Java

Example 2: N=5

Fish Pattern in Java

We break the code into three parts, i.e., upper part, middle part, and lower part. The upper and lower parts are designed over N rows, and the middle part is a single row. We use the following steps to implement the code for the Fish Pattern:

Now, to solve this question, follow the steps below:

  1. First Part
    • for I = 0 to N:
      • As depicted in the above image that first a string, say spaces1having M (initially M=N) spaces appear, then a layer of stars, let's say stars1 having only 1 star, then the string spaces1 appears 2 times (having 2*M spaces) and then another layer of stars, say stars2 has 0 stars initially.
      • Now in each iteration, spaces1 got reduced by a space, stars1 got increased by 2 stars, and stars2 by 1 star.
  2. Middle part:
    • In this row, there is no space, so we just print both stars1and stars2.
  3. Lower Part:
    • We reverse the upper part algorithm to print the lower part.

Let's implement the code for Fish Pattern by using the above steps:

FishPatternExample.java

Output:

Fish Pattern in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA