Print Right Pascal’s Triangle with Increasing Order Repeated Number Pattern
In the previous article, we have discussed Java Program to Print Right Pascal’s Triangle Number Pattern
In this article we are going to see how to print the right Pascal’s triangle with increasing order repeated number pattern.
- Java Code to Print Right Pascal’s Triangle with Increasing Order Repeated Number Pattern
- C to Print Right Pascal’s Triangle with Increasing Order Repeated Number Pattern
- to Print Right Pascal’s Triangle with Increasing Order Repeated Number Pattern
Example-1 When row value=4 1 2 2 3 3 3 4 4 4 4 5 5 5 6 6 7
Example-2: When row value=5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 7 7 7 8 8 9
Now, let’s see the actual program to print it.
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
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 characters of upper half and lower half triangle.
- After each iteration print a new line.
Java Code to Print Right Pascal’s Triangle with Increasing Order Repeated 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, tempHolder=1;
//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(tempHolder+ " ");
}
tempHolder++;
//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(tempHolder+ " ");
}
tempHolder++;
//Prints a newline
System.out.println();
}
}
}
Output: Rows : 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 7 7 7 8 8 9
C to Print Right Pascal’s Triangle with Increasing Order Repeated 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, tempHolder = 1;
//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 ", tempHolder);
}
tempHolder++;
//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 ", tempHolder);
}
tempHolder++;
//Prints a newline
printf("\n");
}
return 0;
}
Output Rows : 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 7 7 7 8 8 9
C++ to Print Right Pascal’s Triangle with Increasing Order Repeated 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, tempHolder = 1;
//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 << tempHolder << " ";
}
//Incrementing the tempHolder variable after each row iteration
tempHolder++;
//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 << tempHolder << " ";
}
//Incrementing the tempHolder variable after each row iteration
tempHolder++;
//Prints a newline
cout << endl;
}
return 0;
}
Output: Rows : 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 7 7 7 8 8 9
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: