Program to Print Seirpinski Tringle Star Pattern
In this article we are going to see how to print Seirpinski tringle star program.
Example-1 When row value=8 * * * * * * * * * * * * * * * * * * * * * * * * * * *
Example-2: When row value=16 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Now, let’s see the actual program to print it.
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Approach:
- Enter total row and store it in an integer variable
row
. - Take first outer for loop to print the row value.
- Take first inner for loop for printing space.
- Take second for loop for printing space according to condition
if ((c & y) != 0)
else it will print star. - Then go on printing the star symbol according to loop.
JAVA Code:
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,y; //creating scanner class object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //outer for loop for ( y = row - 1; y >= 0; y--) { // printing space till the value of y for ( r = 0; r < y; r++) System.out.print(" "); // printing '*' for ( c = 0; c + y < row ; c++) { if ((c & y) != 0) System.out.print(" "); else System.out.print("* "); } System.out.print("\n"); } } }
Output : Enter row : 8 * * * * * * * * * * * * * * * * * * * * * * * * * * *
C Code:
#include <stdio.h> int main() { int r, row, c,y ; printf("Enter rows: "); scanf("%d", &row); for ( y = row - 1; y >= 0; y--) { // printing space till the value of y for ( r = 0; r < y; r++) printf(" "); // printing '*' for ( c = 0; c + y < row ; c++) { if ((c & y) != 0) printf(" " ); else printf("* "); } printf("\n"); } return 0; }
Output : Enter row : 8 * * * * * * * * * * * * * * * * * * * * * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c, y ; cout << "Enter rows: "; cin >> row; for ( y = row - 1; y >= 0; y--) { // printing space till the value of y for ( r = 0; r < y; r++) cout <<" "; // printing '*' for ( c = 0; c + y < row ; c++) { if ((c & y) != 0) cout << " " ; else cout <<"* "; } cout <<"\n"; } return 0; }
Output :
Enter row : 8 * * * * * * * * * * * * * * * * * * * * * * * * * * *
Related Java Star Pattern Programs: