Print Downward Triangle with Increasing Order Number Pattern
In the previous article, we have discussed Java Program to Print Triangle with Increasing Order Repeated Number Pattern
In this article we are going to see how to print the downward triangle with increasing order number pattern.
- Java Code to Print Downward Triangle with Increasing Order Number Pattern
- C Code to Print Downward Triangle with Increasing Order Number Pattern
- C++ Code to Print Downward Triangle with Increasing Order Number Pattern
Example-1 When rows value = 5 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Example-2: When rows value=7 1 2 3 4 5 6 7 1 2 3 4 5 6 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.
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
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 Downward Triangle with Increasing Order 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 1 to the number of rows entered by the user for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++) { System.out.print(" "); } //Inner loop to print number for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows + 1; numberOfColumns++) { System.out.print(numberOfColumns+" "); } //Prints a newline System.out.println(); } } }
Output: Rows : 7 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
C Code to Print Downward Triangle with Increasing Order 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 1 to the number of rows entered by the user for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++) { printf(" "); } //Inner loop to print number for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows + 1; numberOfColumns++) { printf("%d ", numberOfColumns); } //Prints a newline printf("\n"); } return 0; }
Output: Rows : 7 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
C++ Code to Print Downward Triangle with Increasing Order 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 1 to the number of rows entered by the user for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++) { cout << " "; } //Inner loop to print number for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows + 1; numberOfColumns++) { cout << numberOfColumns << " "; } //Prints a newline cout << endl; } return 0; }
Output: Rows : 7 1 2 3 4 5 6 7 1 2 3 4 5 6 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: