# Pascal triangle in java – Java Program to Print Right Pascal’s Triangle Number Pattern

## Print Right Pascal’s Triangle Number Pattern

Pascal triangle in java: In the previous article, we have discussed Java Program to Print Pant Number Pattern(Second Approach)

In this article we are going to see how to print the right Pascal’s triangle number pattern.

Example-1

When row value=4

1
1 2
1 2 3
1 2 3 4
1 2 3
1 2
1
Example-2:

When row value=5

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

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 rows.
• Take one outer for loop to iterate the rows.
• Take two inner for loops, to print the character values of upper half and lower half triangle.
• After each iteration print a new line.

### Java Code to Print Right Pascal’s Triangle Number Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
//Create a new Scanner object
Scanner scan = new Scanner(System.in);

//Taking number of rows as input from user
System.out.print("Rows : ");
int rows = scan.nextInt();

//Row and column are the iterators
int numberOfRows, numberOfColumns;

//Outer loop to iterate the rows (upper half)
//Iterates from 1 to number of rows entered by the user
for (numberOfRows = 1; numberOfRows<=rows; numberOfRows++)
{
//Inner loops to iterate the columns
for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++)
{
//Prints number
System.out.print(numberOfColumns+ " ");
}
//Prints a newline
System.out.println();
}

//Second outer loop to iterate the rows (lower half)
//Iterates from number of rows entered by the user to number 1
for (numberOfRows = rows; numberOfRows>=1; numberOfRows--)
{
//Inner loops to iterate the columns
for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
{
//Prints number
System.out.print(numberOfColumns+ " ");
}
//Prints a newline
System.out.println();
}
}
}


Output:

Rows : 5

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

### C Code to Print Right Pascal’s Triangle Number Pattern

#include <stdio.h>

int main()
{
//Taking total number of rows as input from user
printf("Rows : ");
int rows;
scanf("%d", &rows);

//Row and column are the iterators
int numberOfRows, numberOfColumns;

//Outer loop to iterate the rows (upper half)
//Iterates from 1 to number of rows entered by the user
for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
{
//Inner loops to iterate the columns
for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++)
{
//Prints number
printf("%d ", numberOfColumns);
}
//Prints a newline
printf("\n");
}

//Second outer loop to iterate the rows (lower half)
//Iterates from number of rows entered by the user to number 1
for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
{
//Inner loops to iterate the columns
for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
{
//Prints number
printf("%d ", numberOfColumns);
}
//Prints a newline
printf("\n");
}
return 0;
}

Output:

Rows : 5

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

### C++ Code to Print Right Pascal’s Triangle Number Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
//Taking total number of rows as input from user
printf("Rows : ");
int rows;
cin >> rows;

//Row and column are the iterators
int numberOfRows, numberOfColumns;

//Outer loop to iterate the rows (upper half)
//Iterates from 1 to number of rows entered by the user
for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
{
//Inner loops to iterate the columns
for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++)
{
//Prints number
cout << numberOfColumns << " ";
}
//Prints a newline
cout << endl;
}

//Second outer loop to iterate the rows (lower half)
//Iterates from number of rows entered by the user to number 1
for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
{
//Inner loops to iterate the columns
for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
{
//Prints number
cout << numberOfColumns << " ";
}
//Prints a newline
cout << endl;
}
return 0;
}


Output:

Rows : 5

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

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 Number Pattern Programs: