Program to Print Hollow Right Pascal’s Triangle Star Pattern
Pascal’s triangle java: In this article we are going to see how to print Hollow right pascal’s Tringle star program.
Example-1 When row value=4 * ** * * * * * * ** *
Example-2: When row value=5 * ** * * * * * * * * * * ** *
Now, let’s see the actual program to print it.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach:
- Enter total row and store it in an integer variable row.
- Take first outer for loop to print all rows of upper part .
- Take first inner for loop to print column values i.e., first inner for loop will print according to condition , i.e. if( c == 1 || c == r ) if condition satisfies then it will print star else it will print space.
- Take second outer for loop for printing stars in creasing & decreasing order .
- First inner for loop will print stars in decreasing order with condition if( c == row-1 || c == r || r == row)
- Second inner for loop will print stars in increasing condition .
- 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,d; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //outer for loop for (r =1; r<=row; r++) { for ( c=1; c<=r; c++) { if( c == 1 || c == r ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for ( r=1; r<=row-1; r++) { // Print star in decreasing order for ( c = row-1; c >= r; c--) { if( c == row-1 || c == r || r == row) System.out.print("*"); else System.out.print(" "); } // Print space in increasing order for ( c = 1; c < r; c++) { System.out.print(" "); } 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,d; char random; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering the number of row System.out.print("Enter character : "); random = s.next().charAt(0); //outer for loop for (r =1; r<=row; r++) { for ( c=1; c<=r; c++) { if( c == 1 || c == r ) System.out.print(random); else System.out.print(" "); } System.out.println(); } for ( r=1; r<=row-1; r++) { // Print star in decreasing order for ( c = row-1; c >= r; c--) { if( c == row-1 || c == r || r == row) System.out.print(random); else System.out.print(" "); } // Print space in increasing order for ( c = 1; c < r; c++) { System.out.print(" "); } System.out.println(); } } }
Output : Enter Row: 5 Enter character : $ $ $$ $ $ $ $ $ $ $ $ $ $ $$ $
C Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for (r =1; r<=row; r++) { for ( c=1; c<=r; c++) { if( c == 1 || c == r ) printf("*"); else printf(" "); } printf("\n"); } for ( r=1; r<=row-1; r++) { // Print star in decreasing order for ( c = row-1; c >= r; c--) { if( c == row-1 || c == r || r == row) printf("*"); else printf(" "); } // Print space in increasing order for ( c = 1; c < r; c++) { printf(" "); } printf("\n"); } return 0; }
Output : Enter Row: 5 * ** * * * * * * * * * * ** *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for (r =1; r<=row; r++) { for ( c=1; c<=r; c++) { if( c == 1 || c == r ) cout <<"*"; else cout <<" "; } cout << "\n"; } for ( r=1; r<=row-1; r++) { // Print star in decreasing order for ( c = row-1; c >= r; c--) { if( c == row-1 || c == r || r == row) cout <<"*"; else cout <<" "; } // Print space in increasing order for ( c = 1; c < r; c++) { cout <<" "; } cout << "\n"; } return 0; }
Output : Enter Row: 5 * ** * * * * * * * * * * ** *
Related Java Star Pattern Programs: