Sierpinski triangle c++ – Java Program to Print Sierpinski Triangle Character Pattern

Program to Print Sierpinski triangle Character Pattern

Sierpinski triangle c++: In the previous article, we have discussed Java Program to Print Window Character Pattern. In this article, we are going to see how to print the Sierpinski tringle character program.

Example-1

When rows :  8

          A 
        A  B 
      A     C 
     A B C D 
    A           E 
   A B        E  F 
  A    C     E    G 
 A B C D E F G H

Now, let’s see the actual program to print it.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Approach:

  • Enter total row and store it in an integer variable row. (Prefer multiple of 8)
  • 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 character.
  • Then go on printing the star symbol according to loop.

Java Code to Print Sierpinski Triangle Character Pattern

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,y;
    // Starting ASCII character 65
    int asciiAlpha=65;
    //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
    //it will iterate all the rows
    for (y = row - 1; y >= 0; y--)
    {
            // printing space till the value of y 
            for (r = 0; r < y; r++) 
                System.out.print(" ");
            // printing character
            for (c = 0; c + y < row ; c++) 
            {
                if ((c & y) != 0)
                    System.out.print("  ");
                else
                    System.out.print((char)(c+asciiAlpha)+" ");
            }
             System.out.print("\n");
   }
  }
}
Enter rows :       
       A 
      A B 
     A    C 
    A B C D 
   A          E 
  A  B      E   F 
 A     C   E      G 
A B C D E F G H

C Code to Print Sierpinski Triangle Character Pattern

#include <stdio.h>
int main() 
{
   int r, row, c,y ;
   int asciiAlpha=65;
   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 character
            for ( c = 0; c + y < row ; c++) 
            {
                if ((c & y) != 0)
                     printf("  " );
                else
                     printf("%c ",(c+asciiAlpha));
            }
              printf("\n");
   }
   return 0;
}
Output :

Enter rows :   8
     
        A 
      A  B 
     A     C 
    A B C D 
   A           E 
  A  B       E  F 
 A     C    E     G 
A B C D E F G H

C++ Code to Print Sierpinski Triangle Character Pattern

#include <iostream>
using namespace std;
int main()
{
   int row, r , c, y ;
   int asciiAlpha=65;
   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 character
            for ( c = 0; c + y < row ; c++) 
            {
                if ((c & y) != 0)
cout << "  " ;
                else
                    cout << (char)(c+asciiAlpha)<<" ";
            }
             cout <<"\n";
   }
   return 0;
}
Output:

Enter rows :   8

        A 
      A   B 
     A     C 
    A B C D 
   A           E 
  A  B      E  F 
 A     C   E    G 
A B C D E F G H

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs :