Program to Print Wave Star Pattern
In this article we are going to see how to print wave star program.
Example-1 When row value=5 ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** *
Example-2: When row value=4 ** ** ** ** * * * * * * * * * * * * * * * * * ** ** ** *
Now, let’s see the actual program to print it.
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Approach:
- Enter total row and store it in an integer variable row.
- Take wavelength and wave height equal to row .
- Take first outer for loop to print the height of the wave .
- Take first inner for loop to print space by wave height.
- Take second inner for loop for wavelength .
- Take another first inner for loop under it for checking immediate spaces.
- Take another second inner for loop under it for printing stars according to condition if (c==1) and if (c==a) else it will print space .
- Then go on printing the star symbol according to loop.
JAVA Code:
Method-1: Static Star Character
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,y,a,b,d,n,wh,wl; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //outer for loop a= 2; b = 1; wh=row; wl=row; // loop for height of wave for (r = 0; r < wh; r++) { for (c = wh; c <= wh + r; c++) System.out.print(" "); // loop for wave length for (d = 1; d <= wl; d++) { for (n = (wh + wh - 2); n >= b; n--) System.out.print(" "); for (c = 1; c <= a; c++) { if (c == 1) System.out.print("*"); else if (c == a) System.out.print("*"); else System.out.print(" "); } } // incrementing counters value by two b = b + 2; a = a + 2; System.out.println(); } } }
Output : Enter row : 5 ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** *
Method-2: User Input Character
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,y,a,b,d,n,wh,wl; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering any random character System.out.print("Enter character : "); char wave = s.next().charAt(0); //outer for loop a= 2; b = 1; wh=row; wl=row; // loop for height of wave for (r = 0; r < wh; r++) { for (c = wh; c <= wh + r; c++) System.out.print(" "); // loop for wave length for (d = 1; d <= wl; d++) { for (n = (wh + wh - 2); n >= b; n--) System.out.print(" "); for (c = 1; c <= a; c++) { if (c == 1) System.out.print(wave); else if (c == a) System.out.print(wave); else System.out.print(" "); } } // incrementing counters value by two b = b + 2; a = a + 2; System.out.println(); } } }
Output : Enter row : 5 Enter character : * ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** *
C Code:
#include <stdio.h> int main() { int row,r,c,y,a,b,d,n,wh,wl; printf("Enter rows: "); scanf("%d", &row); a= 2; b = 1; wh=row; wl=row; // loop for height of wave for (r = 0; r < wh; r++) { for (c = wh; c <= wh + r; c++) printf(" "); // loop for wave length for (d = 1; d <= wl; d++) { for (n = (wh + wh - 2); n >= b; n--) printf(" "); for (c = 1; c <= a; c++) { if (c == 1) printf("*"); else if (c == a) printf("*"); else printf(" "); } } // incrementing counters value by two b = b + 2; a = a + 2; printf("\n"); } return 0; }
Output : Enter row : 5 ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** *
C++ Code:
#include <iostream> using namespace std; int main() { int row,r,c,y,a,b,d,n,wh,wl; cout <<"Enter rows: "; cin >> row ; a= 2; b = 1; wh=row; wl=row; // loop for height of wave for (r = 0; r < wh; r++) { for (c = wh; c <= wh + r; c++) cout << " "; // loop for wave length for (d = 1; d <= wl; d++) { for (n = (wh + wh - 2); n >= b; n--) cout << " "; for (c = 1; c <= a; c++) { if (c == 1) cout <<"*"; else if (c == a) cout <<"*"; else cout <<" "; } } // incrementing counters value by two b = b + 2; a = a + 2; printf("\n"); } return 0; }
Output : Enter row : 5 ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** *
Related Java Star Pattern Programs: