Open In App

Program to print Sine-Wave Pattern

Last Updated : 09 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two integers waveHeight and waveLength. The task is to print a sine wave pattern using the character 0. The sine wave rises and falls vertically across the given height and repeats horizontally for the given length. 

Examples:

Input: waveHeight = 5, waveLength = 10
Output: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Input: waveHeight = 3, waveLength = 4
Output:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

Approach: 

The idea is to simulate a sine wave pattern row by row using nested loops. The main observation is that a wave can be broken into repeating segments, and by carefully adjusting gaps, one can simulate the curve. The approach builds the pattern row-wise, dynamically changing distances to maintain the flow.

Steps to implement the above idea:

  • Initialize innerSpacing and outerSpacing to control the horizontal layout of the wave pattern.
  • Use a loop from 1 to waveHeight to print each row of the sine wave.
  • Inside that, loop from 1 to waveLength to repeat wave segments horizontally.
  • Print outerSpacing number of spaces, then a character or dot representing the first wave point.
  • Print innerSpacing spaces followed by the second character or dot, then again outerSpacing spaces.
  • After each wave segment, print a single space to separate from the next repetition.
  • Adjust innerSpacing and outerSpacing based on the current row to shape the wave correctly.


C++
// C++ program to print sine wave pattern
#include <bits/stdc++.h>
using namespace std;

// Function to print sine wave pattern
void printSineWave(int waveHeight, int waveLength) {

    // innerSpacing: spaces between two '0's inside the wave
    // outerSpacing: spaces before and after each wave segment
    int innerSpacing = 1;
    int outerSpacing = 2;

    // Loop for each row (height of the wave)
    for (int row = 1; row <= waveHeight; row++) {

        // Loop for each wave repetition (length)
        for (int col = 1; col <= waveLength; col++) {

            // Print leading outer spacing
            for (int i = 1; i <= outerSpacing; i++) {
                cout << " ";
            }

            // First wave dot
            cout << "0";

            // Print inner spacing
            for (int i = 1; i <= innerSpacing; i++) {
                cout << " ";
            }

            // Second wave dot
            cout << "0";

            // Print trailing outer spacing
            for (int i = 1; i <= outerSpacing; i++) {
                cout << " ";
            }

            cout << " ";
        }

        // Adjust spacing for next row based on wave shape
        if (row + 1 != waveHeight) {
            outerSpacing = 1;
            innerSpacing = 3;
        } else {
            outerSpacing = 0;
            innerSpacing = 5;
        }

        cout << "\n";
    }
}

// Driver code
int main() {
    int waveHeight = 3;
    int waveLength = 4;

    printSineWave(waveHeight, waveLength);

    return 0;
}
C
// C program to print sine wave pattern
#include <stdio.h>

// Function to print sine wave pattern
void printSineWave(int waveHeight, int waveLength) {

    // innerSpacing: spaces between two '0's inside the wave
    // outerSpacing: spaces before and after each wave segment
    int innerSpacing = 1;
    int outerSpacing = 2;

    // Loop for each row (height of the wave)
    for (int row = 1; row <= waveHeight; row++) {

        // Loop for each wave repetition (length)
        for (int col = 1; col <= waveLength; col++) {

            // Print leading outer spacing
            for (int i = 1; i <= outerSpacing; i++) {
                printf(" ");
            }

            // First wave dot
            printf("0");

            // Print inner spacing
            for (int i = 1; i <= innerSpacing; i++) {
                printf(" ");
            }

            // Second wave dot
            printf("0");

            // Print trailing outer spacing
            for (int i = 1; i <= outerSpacing; i++) {
                printf(" ");
            }

            printf(" ");
        }

        // Adjust spacing for next row based on wave shape
        if (row + 1 != waveHeight) {
            outerSpacing = 1;
            innerSpacing = 3;
        } else {
            outerSpacing = 0;
            innerSpacing = 5;
        }

        printf("\n");
    }
}

// Driver code
int main() {
    int waveHeight = 3;
    int waveLength = 4;

    printSineWave(waveHeight, waveLength);

    return 0;
}
Java
// Java program to print sine wave pattern
class GfG {

    // Function to print sine wave pattern
    static void printSineWave(int waveHeight, int waveLength) {

        // innerSpacing: spaces between two '0's inside the wave
        // outerSpacing: spaces before and after each wave segment
        int innerSpacing = 1;
        int outerSpacing = 2;

        // Loop for each row (height of the wave)
        for (int row = 1; row <= waveHeight; row++) {

            // Loop for each wave repetition (length)
            for (int col = 1; col <= waveLength; col++) {

                // Print leading outer spacing
                for (int i = 1; i <= outerSpacing; i++) {
                    System.out.print(" ");
                }

                // First wave dot
                System.out.print("0");

                // Print inner spacing
                for (int i = 1; i <= innerSpacing; i++) {
                    System.out.print(" ");
                }

                // Second wave dot
                System.out.print("0");

                // Print trailing outer spacing
                for (int i = 1; i <= outerSpacing; i++) {
                    System.out.print(" ");
                }

                System.out.print(" ");
            }

            // Adjust spacing for next row based on wave shape
            if (row + 1 != waveHeight) {
                outerSpacing = 1;
                innerSpacing = 3;
            } else {
                outerSpacing = 0;
                innerSpacing = 5;
            }

            System.out.println();
        }
    }

    // Driver code
    public static void main(String[] args) {
        int waveHeight = 3;
        int waveLength = 4;

        printSineWave(waveHeight, waveLength);
    }
}
Python
# Python program to print sine wave pattern

# Function to print sine wave pattern
def printSineWave(waveHeight, waveLength):

    # innerSpacing: spaces between two '0's inside the wave
    # outerSpacing: spaces before and after each wave segment
    innerSpacing = 1
    outerSpacing = 2

    # Loop for each row (height of the wave)
    for row in range(1, waveHeight + 1):

        # Loop for each wave repetition (length)
        for col in range(1, waveLength + 1):

            # Print leading outer spacing
            for i in range(1, outerSpacing + 1):
                print(" ", end="")

            # First wave dot
            print("0", end="")

            # Print inner spacing
            for i in range(1, innerSpacing + 1):
                print(" ", end="")

            # Second wave dot
            print("0", end="")

            # Print trailing outer spacing
            for i in range(1, outerSpacing + 1):
                print(" ", end="")

            print(" ", end="")

        # Adjust spacing for next row based on wave shape
        if row + 1 != waveHeight:
            outerSpacing = 1
            innerSpacing = 3
        else:
            outerSpacing = 0
            innerSpacing = 5

        print()

# Driver code
if __name__=="__main__":
    waveHeight = 3
    waveLength = 4

    printSineWave(waveHeight, waveLength)
C#
// C# program to print sine wave pattern
using System;

class GfG {

    // Function to print sine wave pattern
    static void printSineWave(int waveHeight, int waveLength) {

        // innerSpacing: spaces between two '0's inside the wave
        // outerSpacing: spaces before and after each wave segment
        int innerSpacing = 1;
        int outerSpacing = 2;

        // Loop for each row (height of the wave)
        for (int row = 1; row <= waveHeight; row++) {

            // Loop for each wave repetition (length)
            for (int col = 1; col <= waveLength; col++) {

                // Print leading outer spacing
                for (int i = 1; i <= outerSpacing; i++) {
                    Console.Write(" ");
                }

                // First wave dot
                Console.Write("0");

                // Print inner spacing
                for (int i = 1; i <= innerSpacing; i++) {
                    Console.Write(" ");
                }

                // Second wave dot
                Console.Write("0");

                // Print trailing outer spacing
                for (int i = 1; i <= outerSpacing; i++) {
                    Console.Write(" ");
                }

                Console.Write(" ");
            }

            // Adjust spacing for next row based on wave shape
            if (row + 1 != waveHeight) {
                outerSpacing = 1;
                innerSpacing = 3;
            } else {
                outerSpacing = 0;
                innerSpacing = 5;
            }

            Console.WriteLine();
        }
    }

    // Driver code
    public static void Main(string[] args) {
        int waveHeight = 3;
        int waveLength = 4;

        printSineWave(waveHeight, waveLength);
    }
}
JavaScript
// JavaScript program to print sine wave pattern

// Function to print sine wave pattern
function printSineWave(waveHeight, waveLength) {

    // innerSpacing: spaces between two '0's inside the wave
    // outerSpacing: spaces before and after each wave segment
    let innerSpacing = 1;
    let outerSpacing = 2;

    // Loop for each row (height of the wave)
    for (let row = 1; row <= waveHeight; row++) {

        let line = "";

        // Loop for each wave repetition (length)
        for (let col = 1; col <= waveLength; col++) {

            // Print leading outer spacing
            for (let i = 1; i <= outerSpacing; i++) {
                line += " ";
            }

            // First wave dot
            line += "0";

            // Print inner spacing
            for (let i = 1; i <= innerSpacing; i++) {
                line += " ";
            }

            // Second wave dot
            line += "0";

            // Print trailing outer spacing
            for (let i = 1; i <= outerSpacing; i++) {
                line += " ";
            }

            line += " ";
        }

        console.log(line);

        // Adjust spacing for next row based on wave shape
        if (row + 1 != waveHeight) {
            outerSpacing = 1;
            innerSpacing = 3;
        } else {
            outerSpacing = 0;
            innerSpacing = 5;
        }
    }
}

// Driver code
let waveHeight = 3;
let waveLength = 4;

printSineWave(waveHeight, waveLength);

Output
  0 0     0 0     0 0     0 0   
 0   0   0   0   0   0   0   0  
0     0 0     0 0     0 0     0 

Time Complexity: O(waveHeight*waveLength), Nested loops iterate over each wave row and segment.
Space Complexity: O(1), No extra space used apart from a few variables (constant space).

Printing Sine-Wave of Alphabets

The problem remains the same, but this time the task is to print a sine wave pattern using uppercase alphabetical letters (A to Z) instead of the digit 0.

Example:

Input: waveHeight = 3, waveLength = 4
Output:
C D I J O P U V
B E H K N Q T W
A F G L M R S X

Approach:

The idea is to extend the wave pattern logic from simple 0s to alphabets. The core observation is that each wave segment needs two letters with a specific step difference. While the spacing logic remains similar to the previous 0-based version, the added complexity lies in maintaining letter sequences and wrapping around after 'Z'.

Steps to implement the above idea:

  • Initialize innerSpacing, outerSpacing, inc, and jump based on waveHeight to control gaps and character steps.
  • Set starting character ch using 'A' + waveHeight - 1 to begin from bottom of sine structure.
  • Loop through each row from 1 to waveHeight to simulate vertical structure of the wave.
  • For each row, loop through waveLength to repeat the pattern horizontally across the wave.
  • Print outerSpacing, then first character, followed by innerSpacing and second character, adjusting ASCII when past 'Z'.
  • Update ch with jump after each segment to get the next starting character in the series.
  • At end of row, update spacing, ch, inc, and jump to shift wave formation for the next level.
C++
// C++ program to print sine wave pattern using letters
#include <bits/stdc++.h>
using namespace std;

// Function to print sine wave pattern
void printSineWave(int waveHeight, int waveLength) {

    // innerSpacing: spaces between the two characters in a segment
    // outerSpacing: spaces before and after each wave segment
    int innerSpacing = 1, outerSpacing = 2;

    // inc determines character step for second letter
    int inc = 1;

    // jump determines the character step after a wave segment
    int jump = (waveHeight * 3) - (waveHeight + 1);
  
    char ch = 'A' + waveHeight - 1;

    // Loop for each row (height of the wave)
    for (int i = 1; i <= waveHeight; i++) {

        // Loop for each wave repetition (length)
        for (int j = 1; j <= waveLength; j++) {

            // Print leading outer spacing
            for (int k = 1; k <= outerSpacing; k++) 
                cout << " ";

            // Print first wave character
            cout << ch;

            // Print inner spacing
            for (int k = 1; k <= innerSpacing; k++) 
                cout << " ";

            // Update character for second wave dot
            ch += inc;
            if (ch > 'Z') ch -= 26;

            // Print second wave character
            cout << ch;

            // Print trailing outer spacing
            for (int k = 1; k <= outerSpacing; k++) 
                cout << " ";

            cout << " ";

            // Update character for next segment
            ch += jump;
            if (ch > 'Z') ch -= 26;
        }

        // Update outer and inner spacing for next row
        outerSpacing = (i + 1 != waveHeight) ? 1 : 0;
        innerSpacing = (i + 1 != waveHeight) ? 3 : 5;

        // Reset starting character for next row
        ch = 'A' + waveHeight - i - 1;

        inc += 2;
        jump -= 2;

        cout << "\n";
    }
}

// Driver code
int main() {
    int waveHeight = 3;
    int waveLength = 4;

    printSineWave(waveHeight, waveLength);

    return 0;
}
C
// C program to print sine wave pattern using letters
#include <stdio.h>

// Function to print sine wave pattern
void printSineWave(int waveHeight, int waveLength) {

    // innerSpacing: spaces between the two characters in a segment
    // outerSpacing: spaces before and after each wave segment
    int innerSpacing = 1, outerSpacing = 2;

    // inc determines character step for second letter
    int inc = 1;

    // jump determines the character step after a wave segment
    int jump = (waveHeight * 3) - (waveHeight + 1);

    char ch = 'A' + waveHeight - 1;

    // Loop for each row (height of the wave)
    for (int i = 1; i <= waveHeight; i++) {

        // Loop for each wave repetition (length)
        for (int j = 1; j <= waveLength; j++) {

            // Print leading outer spacing
            for (int k = 1; k <= outerSpacing; k++) 
                printf(" ");

            // Print first wave character
            printf("%c", ch);

            // Print inner spacing
            for (int k = 1; k <= innerSpacing; k++) 
                printf(" ");

            // Update character for second wave dot
            ch += inc;
            if (ch > 'Z') ch -= 26;

            // Print second wave character
            printf("%c", ch);

            // Print trailing outer spacing
            for (int k = 1; k <= outerSpacing; k++) 
                printf(" ");

            printf(" ");

            // Update character for next segment
            ch += jump;
            if (ch > 'Z') ch -= 26;
        }

        // Update outer and inner spacing for next row
        outerSpacing = (i + 1 != waveHeight) ? 1 : 0;
        innerSpacing = (i + 1 != waveHeight) ? 3 : 5;

        // Reset starting character for next row
        ch = 'A' + waveHeight - i - 1;

        inc += 2;
        jump -= 2;

        printf("\n");
    }
}

// Driver code
int main() {
    int waveHeight = 3;
    int waveLength = 4;

    printSineWave(waveHeight, waveLength);

    return 0;
}
Java
// Java program to print sine wave pattern using letters
import java.util.*;

class GfG {

    // Function to print sine wave pattern
    static void printSineWave(int waveHeight, int waveLength) {

        // innerSpacing: spaces between the two characters in a segment
        // outerSpacing: spaces before and after each wave segment
        int innerSpacing = 1, outerSpacing = 2;

        // inc determines character step for second letter
        int inc = 1;

        // jump determines the character step after a wave segment
        int jump = (waveHeight * 3) - (waveHeight + 1);
      
        char ch = (char)('A' + waveHeight - 1);

        // Loop for each row (height of the wave)
        for (int i = 1; i <= waveHeight; i++) {

            // Loop for each wave repetition (length)
            for (int j = 1; j <= waveLength; j++) {

                // Print leading outer spacing
                for (int k = 1; k <= outerSpacing; k++) 
                    System.out.print(" ");

                // Print first wave character
                System.out.print(ch);

                // Print inner spacing
                for (int k = 1; k <= innerSpacing; k++) 
                    System.out.print(" ");

                // Update character for second wave dot
                ch += inc;
                if (ch > 'Z') ch -= 26;

                // Print second wave character
                System.out.print(ch);

                // Print trailing outer spacing
                for (int k = 1; k <= outerSpacing; k++) 
                    System.out.print(" ");

                System.out.print(" ");

                // Update character for next segment
                ch += jump;
                if (ch > 'Z') ch -= 26;
            }

            // Update outer and inner spacing for next row
            outerSpacing = (i + 1 != waveHeight) ? 1 : 0;
            innerSpacing = (i + 1 != waveHeight) ? 3 : 5;

            // Reset starting character for next row
            ch = (char)('A' + waveHeight - i - 1);

            inc += 2;
            jump -= 2;

            System.out.println();
        }
    }

    public static void main(String[] args) {
        int waveHeight = 3;
        int waveLength = 4;

        printSineWave(waveHeight, waveLength);
    }
}
Python
# Python program to print sine wave pattern using letters

# Function to print sine wave pattern
def printSineWave(waveHeight, waveLength):

    # innerSpacing: spaces between the two characters in a segment
    # outerSpacing: spaces before and after each wave segment
    innerSpacing = 1
    outerSpacing = 2

    # inc determines character step for second letter
    inc = 1

    # jump determines the character step after a wave segment
    jump = (waveHeight * 3) - (waveHeight + 1)
  
    ch = ord('A') + waveHeight - 1

    # Loop for each row (height of the wave)
    for i in range(1, waveHeight + 1):

        # Loop for each wave repetition (length)
        for j in range(1, waveLength + 1):

            # Print leading outer spacing
            print(" " * outerSpacing, end="")

            # Print first wave character
            print(chr(ch), end="")

            # Print inner spacing
            print(" " * innerSpacing, end="")

            # Update character for second wave dot
            ch += inc
            if ch > ord('Z'):
                ch -= 26

            # Print second wave character
            print(chr(ch), end="")

            # Print trailing outer spacing
            print(" " * outerSpacing, end=" ")

            # Update character for next segment
            ch += jump
            if ch > ord('Z'):
                ch -= 26

        # Update outer and inner spacing for next row
        outerSpacing = 1 if i + 1 != waveHeight else 0
        innerSpacing = 3 if i + 1 != waveHeight else 5

        # Reset starting character for next row
        ch = ord('A') + waveHeight - i - 1

        inc += 2
        jump -= 2

        print()

if __name__=="__main__":
    waveHeight = 3
    waveLength = 4

    printSineWave(waveHeight, waveLength)
C#
// C# program to print sine wave pattern using letters
using System;

class GfG {

    // Function to print sine wave pattern
    static void printSineWave(int waveHeight, int waveLength) {

        // innerSpacing: spaces between the two characters in a segment
        // outerSpacing: spaces before and after each wave segment
        int innerSpacing = 1, outerSpacing = 2;

        // inc determines character step for second letter
        int inc = 1;

        // jump determines the character step after a wave segment
        int jump = (waveHeight * 3) - (waveHeight + 1);
      
        char ch = (char)('A' + waveHeight - 1);

        // Loop for each row (height of the wave)
        for (int i = 1; i <= waveHeight; i++) {

            // Loop for each wave repetition (length)
            for (int j = 1; j <= waveLength; j++) {

                // Print leading outer spacing
                for (int k = 1; k <= outerSpacing; k++) 
                    Console.Write(" ");

                // Print first wave character
                Console.Write(ch);

                // Print inner spacing
                for (int k = 1; k <= innerSpacing; k++) 
                    Console.Write(" ");

                // Update character for second wave dot
                ch += (char)inc;
                if (ch > 'Z') ch = (char)(ch - 26);

                // Print second wave character
                Console.Write(ch);

                // Print trailing outer spacing
                for (int k = 1; k <= outerSpacing; k++) 
                    Console.Write(" ");

                Console.Write(" ");

                // Update character for next segment
                ch += (char)jump;
                if (ch > 'Z') ch = (char)(ch - 26);
            }

            // Update outer and inner spacing for next row
            outerSpacing = (i + 1 != waveHeight) ? 1 : 0;
            innerSpacing = (i + 1 != waveHeight) ? 3 : 5;

            // Reset starting character for next row
            ch = (char)('A' + waveHeight - i - 1);

            inc += 2;
            jump -= 2;

            Console.WriteLine();
        }
    }

    static void Main() {
        int waveHeight = 3;
        int waveLength = 4;

        printSineWave(waveHeight, waveLength);
    }
}
JavaScript
// JavaScript program to print sine wave pattern using letters

// Function to print sine wave pattern
function printSineWave(waveHeight, waveLength) {

    // innerSpacing: spaces between the two characters in a segment
    // outerSpacing: spaces before and after each wave segment
    let innerSpacing = 1, outerSpacing = 2;

    // inc determines character step for second letter
    let inc = 1;

    // jump determines the character step after a wave segment
    let jump = (waveHeight * 3) - (waveHeight + 1);
  
    let ch = 'A'.charCodeAt(0) + waveHeight - 1;

    // Loop for each row (height of the wave)
    for (let i = 1; i <= waveHeight; i++) {

        let line = "";

        // Loop for each wave repetition (length)
        for (let j = 1; j <= waveLength; j++) {

            // Print leading outer spacing
            line += " ".repeat(outerSpacing);

            // Print first wave character
            line += String.fromCharCode(ch);

            // Print inner spacing
            line += " ".repeat(innerSpacing);

            // Update character for second wave dot
            ch += inc;
            if (ch > 'Z'.charCodeAt(0)) ch -= 26;

            // Print second wave character
            line += String.fromCharCode(ch);

            // Print trailing outer spacing
            line += " ".repeat(outerSpacing);
            line += " ";

            // Update character for next segment
            ch += jump;
            if (ch > 'Z'.charCodeAt(0)) ch -= 26;
        }

        console.log(line);

        // Update outer and inner spacing for next row
        outerSpacing = (i + 1 != waveHeight) ? 1 : 0;
        innerSpacing = (i + 1 != waveHeight) ? 3 : 5;

        // Reset starting character for next row
        ch = 'A'.charCodeAt(0) + waveHeight - i - 1;

        inc += 2;
        jump -= 2;
    }
}

// Driver Code
let waveHeight = 3;
let waveLength = 4;

printSineWave(waveHeight, waveLength);

Output
  C D     I J     O P     U V   
 B   E   H   K   N   Q   T   W  
A     F G     L M     R S     X 

Time Complexity: O(waveHeight*waveLength), Nested loops iterate over each wave row and segment.
Space Complexity: O(1), No extra space used apart from a few variables (constant space).


Next Article

Similar Reads