Open In App

Program to print the Fish Pattern

Last Updated : 23 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to print a pattern of fish over 2N+1 rows.

Example:

Input: N=3
Output:
        *      
      ***    *
   *****  **
**********
   *****  **
      ***    *
        *      

Input: N=5
Output:
              *          
            ***        *
         *****      **
      *******    ***
   *********  ****
****************
   *********  ****
      *******    ***
         *****      **
            ***        *
              *     

Approach: The fish consists of three parts:

  • Upper Part: Over N rows.
  • Middle Part: Singe row in the middle
  • Lower Part: Over N rows

Now, let’s try to understand the pattern using an example:
For N=3, the fish is:

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

  1. First, create the upper part:
    • Run a loop for i=0 to i<N, and in each iteration of the loop:
      • As depicted in the above image that first a string, say spaces1 having 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 having 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. For the middle part, just print both stars1 and stars2, as no spaces appear in this row.
  3. Now, to get the lower part, reverse the algorithm for the upper part.
  4. After the loop ends, the pattern of fish will be created.

Below is the implementation of the above approach.

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the pattern of a fish
// over N rows
void printFish(int N)
{
 
    string spaces1 = "", spaces2 = "";
    string stars1 = "*", stars2 = "";
    for (int i = 0; i < N; ++i) {
        spaces1 += ' ';
    }
    spaces2 = spaces1;
 
    for (int i = 0; i < 2 * N + 1; ++i) {
        // For upper part
        if (i < N) {
            cout << spaces1 << stars1
                 << spaces1 << spaces1
                 << stars2 << endl;
            spaces1.pop_back();
            stars1 += "**";
            stars2 += "*";
        }
 
        // For middle part
        if (i == N) {
            cout << spaces1 << stars1
                 << spaces1 << spaces1
                 << stars2 << endl;
        }
 
        // For lower part
        if (i > N) {
            spaces1 += ' ';
            stars1.pop_back();
            stars1.pop_back();
            stars2.pop_back();
            cout << spaces1 << stars1
                 << spaces1 << spaces1
                 << stars2 << endl;
        }
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    printFish(N);
}


Java




// Java program for the above approach
class GFG {
 
    // Function to print the pattern of a fish
    // over N rows
    public static void printFish(int N) {
 
        String spaces1 = "";
        String stars1 = "*", stars2 = "";
        for (int i = 0; i < N; ++i) {
            spaces1 += ' ';
        }
 
        for (int i = 0; i < 2 * N + 1; ++i)
        {
           
            // For upper part
            if (i < N) {
                System.out.print(spaces1 + stars1 + spaces1 + spaces1);
                System.out.println(stars2);
                spaces1 = spaces1.substring(0, spaces1.length() - 1);
                stars1 += "**";
                stars2 += "*";
            }
 
            // For middle part
            if (i == N) {
                System.out.print(spaces1 + stars1 + spaces1 + spaces1);
                System.out.println(stars2);
            }
 
            // For lower part
            if (i > N) {
                spaces1 += ' ';
                stars1 = stars1.substring(0, stars1.length() - 1);
                stars1 = stars1.substring(0, stars1.length() - 1);
                stars2 = stars2.substring(0, stars2.length() - 1);
                System.out.print(spaces1 + stars1 + spaces1 + spaces1);
                System.out.println(stars2);
            }
        }
    }
 
    // Driver Code
    public static void main(String args[]) {
        int N = 5;
        printFish(N);
    }
}
 
// This code is contributed by gfgking.


Python3




# Python3 program for the above approach
 
# Function to print the pattern of a fish
# over N rows
def printFish(N) :
 
    spaces1 = ""; spaces2 = "";
    stars1 = "*"; stars2 = "";
    for i in range(N) :
        spaces1 += ' ';
 
    spaces2 = spaces1;
 
    for i in range( 2 * N + 1) :
        # For upper part
        if (i < N) :
            print(spaces1,end="");
            print(stars1,end="");
            print(spaces1,end="");
            print(spaces1,end="");
            print(stars2);
            spaces1 = spaces1[:-1]
            stars1 += "**";
            stars2 += "*";
 
        # For middle part
        if (i == N) :
            print(spaces1,end="");
            print(stars1,end="");
            print(spaces1,end="");
            print(spaces1,end="");
            print(stars2);
 
        # For lower part
        if (i > N) :
            spaces1 += ' ';
            stars1 = stars1[:-1];
            stars1 = stars1[:-1];
            stars2 = stars2[:-1];
             
            print(spaces1,end="");
            print(stars1,end="")
            print(spaces1,end="");
            print(spaces1,end="");
            print(stars2);
  
# Driver Code
if __name__ == "__main__" :
 
    N = 5;
    printFish(N);
 
    # This code is contributed by AnkThon


C#




// C# program for the above approach
using System;
class GFG {
 
    // Function to print the pattern of a fish
    // over N rows
    static void printFish(int N) {
 
        string spaces1 = "";
        string stars1 = "*", stars2 = "";
        for (int i = 0; i < N; ++i) {
            spaces1 += ' ';
        }
 
        for (int i = 0; i < 2 * N + 1; ++i)
        {
           
            // For upper part
            if (i < N) {
                Console.Write(spaces1 + stars1 + spaces1 + spaces1);
                Console.Write(stars2 + "\n");
                spaces1 = spaces1.Substring(0, spaces1.Length - 1);
                stars1 += "**";
                stars2 += "*";
            }
 
            // For middle part
            if (i == N) {
                Console.Write(spaces1 + stars1 + spaces1 + spaces1);
                Console.Write(stars2 + "\n");
            }
 
            // For lower part
            if (i > N) {
                spaces1 += ' ';
                stars1 = stars1.Substring(0, stars1.Length - 1);
                stars1 = stars1.Substring(0, stars1.Length - 1);
                stars2 = stars2.Substring(0, stars2.Length - 1);
                Console.Write(spaces1 + stars1 + spaces1 + spaces1);
                Console.Write(stars2 + "\n");
            }
        }
    }
 
    // Driver Code
    public static void Main() {
        int N = 5;
        printFish(N);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
    // JavaScript program for the above approach
 
    // Function to print the pattern of a fish
    // over N rows
    const printFish = (N) => {
 
        let spaces1 = "", spaces2 = "";
        let stars1 = "*", stars2 = "";
        for (let i = 0; i < N; ++i) {
            spaces1 += "  ";
        }
        spaces2 = spaces1;
 
        for (let i = 0; i < 2 * N + 1; ++i) {
            // For upper part
            if (i < N) {
                document.write(`${spaces1}${stars1}${spaces1}${spaces1}${stars2}<br/>`);
                spaces1 = spaces1.substr(0, spaces1.length - 10);
                stars1 += "**";
                stars2 += "*";
            }
 
            // For middle part
            if (i == N) {
                document.write(`${spaces1}${stars1}${spaces1}${spaces1}${stars2}<br/>`);
            }
 
            // For lower part
            if (i > N) {
                spaces1 += "  ";
                stars1 = stars1.substr(0, stars1.length - 2);
                stars2 = stars2.substr(0, stars2.length - 1);
                document.write(`${spaces1}${stars1}${spaces1}${spaces1}${stars2}<br/>`);
            }
        }
    }
 
    // Driver Code
    let N = 5;
    printFish(N);
 
    // This code is contributed by rakeshsahni
 
</script>


 
 

Output

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

 

Time Complexity: O(N)
Auxiliary Space: O(N)

 



Similar Reads

Python Program to print digit pattern
The program must accept an integer N as the input. The program must print the desired pattern as shown in the example input/ output. Examples: Input : 41325 Output : |**** |* |*** |** |***** Explanation: for a given integer print the number of *'s that are equivalent to each digit in the integer. Here the first digit is 4 so print four *sin the fir
3 min read
Program to print interesting pattern
Program to print following pattern: ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** ****5*5*5*5*5**** ***6*6*6*6*6*6*** **7*7*7*7*7*7*7** Examples : Input : 4 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** Input :5 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4**
10 min read
Program to print number pattern
We have to print a pattern where in middle column contains only 1, right side columns contain constant digit which is greater than 1 and left side columns contains constant digit which is greater than 1. Every row should look like a Palindrome. Examples : Input : 3 Output : 1 2 1 2 1 Input : 5 Output : 1 2 1 2 3 2 1 2 3 2 1 2 1 Below is the impleme
7 min read
Program to print number with star pattern
We have to print the pattern as given in the below example.Examples : Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1 C/C++ Code #include &lt;iostream&gt; using namespace std; // C++ program to print above pattern void display(int n) { // 'sp' used for space and 'st' used for star int sp = n
6 min read
Program to print numbers with diamond pattern
write a program where each column represents same number according to given example:Examples : Input : 5 Output : 1 212 32123 212 1 Input : 7 Output : 1 212 32123 4321234 32123 212 1 C/C++ Code // C++ program to print diamond pattern #include&lt;iostream&gt; using namespace std; void display(int n) { // sp stands for space // st stands for number i
6 min read
Program to print the pattern 'G'
In this article, we will learn how to print the pattern G using stars and white-spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program, we have used the simple logic of iteration over l
6 min read
Program to print the pattern ā€˜Dā€™
In this article, we will learn how to print the pattern D using stars and white-spaces. Given a number n, we will write a program to print the pattern D over n lines or rows.Examples : Input : n = 9 Output : ****** * * * * * * * * * * * * * * ****** Input : n = 12 Output : ********* * * * * * * * * * * * * * * * * * * * * ********* If we try to ana
6 min read
Program to print double headed arrow pattern
Given an integer N which is the number of rows, the task is to draw the number pattern in the shape of a double headed arrow.Prerequisite: The pattern is a grow and shrink type pattern and hence basic knowledge to execute loops is required to understand the topic and the code in any language. The geometric shape can be visualized as- Examples: Inpu
9 min read
Program to print V and inverted-V pattern
Inverted V pattern: Given the value of n, print the inverted V pattern.Examples : Input : n = 5 Output : E D D C C B B A A Input : n = 7 Output : G F F E E D D C C B B A A Below is the program to print the above pattern C/C++ Code // C++ Implementation to print the pattern #include &lt;bits/stdc++.h&gt; using namespace std; // Function definition v
8 min read
Program to print the arrow pattern
Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C/C++ Code // C++ program to print the // arrow pattern #include &lt;bits/stdc++.h&gt; using namespace
10 min read