Program to print the Fish Pattern

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:
- 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.
- Run a loop for i=0 to i<N, and in each iteration of the loop:
- For the middle part, just print both stars1 and stars2, as no spaces appear in this row.
- Now, to get the lower part, reverse the algorithm for the upper part.
- 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 rowsvoid 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 Codeint main(){ int N = 5; printFish(N);} |
Java
// Java program for the above approachclass 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 rowsdef 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 Codeif __name__ == "__main__" : N = 5; printFish(N); # This code is contributed by AnkThon |
C#
// C# program for the above approachusing 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)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




