Print Triangle with Increasing Order Repeated Number Pattern
How to print triangle in java: In the previous article, we have discussed Java Program to Print Cross Number Pattern
In this article we are going to see how to print the triangle with increasing order repeated number pattern.
- Java Code to Print Triangle with Increasing Order Repeated Number Pattern
- C Code to Print Triangle with Increasing Order Repeated Number Pattern
- C++ Code to Print Triangle with Increasing Order Repeated Number Pattern
Example-1 When rows value = 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Example-2: When rows value=7 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7
Now, let’s see the actual program to print it.
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Approach:
- Enter total number of rows and store it in an integer variable rows
- Take one outer for loop to iterate the rows.
- Take two inner for loops, one to print the space and the other to print number.
- After each iteration print a new line.
Java Code to Print 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 total 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 //Iterates from 0 to one less than number of rows entered by the user for (numberOfRows = 0; numberOfRows < rows; numberOfRows++) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows; numberOfColumns++) { System.out.print(" "); } //Inner loop to print number for (numberOfColumns = 0; numberOfColumns <= numberOfRows; numberOfColumns++) { System.out.print((numberOfRows + 1)+" "); } //Prints a newline System.out.println(); } } }
Output Rows : 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
C Code to Print 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; //Outer loop to iterate the rows //Iterates from 0 to one less than number of rows entered by the user for (numberOfRows = 0; numberOfRows < rows; numberOfRows++) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows; numberOfColumns++) { printf(" "); } //Inner loop to print number for (numberOfColumns = 0; numberOfColumns <= numberOfRows; numberOfColumns++) { printf("%d ", numberOfRows + 1); } //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
C++ Code to Print 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 cout << "Rows : "; int rows; cin >> rows; //Row and column are the iterators int numberOfRows, numberOfColumns; //Outer loop to iterate the rows //Iterates from 0 to one less than number of rows entered by the user for (numberOfRows = 0; numberOfRows < rows; numberOfRows++) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows; numberOfColumns++) { cout << " "; } //Inner loop to print number for (numberOfColumns = 0; numberOfColumns <= numberOfRows; numberOfColumns++) { cout << numberOfRows + 1 << " "; } //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
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: