Print Floyd’s Triangle Number Pattern
In the previous article, we have discussed Java Program to Print Full Pyramid of Number Pattern
In this article we are going to see how to print Floyd’s triangle number pattern.
- Java Code to Print Floyd’s Triangle Number Pattern
- C Code to Print Floyd’s Triangle Number Pattern
- C++ Code to Print Floyd’s Triangle Number Pattern
Example-1 When rows value = 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example-2: When rows value=7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Now, let’s see the actual program to print it.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach:
- Enter total number of rows and store it in an integer variable
rows
. - Take one outer for loop to iterate the rows.
- Take one inner for loop to iterate the columns.
- After each iteration print a new line.
Java Code to Print Floyd’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 total number of rows as input from user System.out.print("Rows : "); int rows= scan.nextInt(); //Row and column are the iterators and counter to print int numberOfRows, numberOfColumns, counter = 0; //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 number //Iterator iterates from 1 to the numberOfRows for (numberOfColumns = 1; numberOfColumns <= numberOfRows; ++numberOfColumns) { System.out.print(++counter+ " "); } //Prints a newline System.out.println(); } } }
Output Rows : 4 1 2 3 4 5 6 7 8 9 10
C Code to Print Floyd’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 and counter to print int numberOfRows, numberOfColumns, counter = 0; //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 number //Iterator iterates from 1 to the numberOfRows for (numberOfColumns = 1; numberOfColumns <= numberOfRows; ++numberOfColumns) { printf("%d ", ++counter); } //Prints a newline printf("\n"); } return 0; }
Output Rows : 4 1 2 3 4 5 6 7 8 9 10
C++ Code to Print Floyd’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 cout << "Rows : "; int rows; cin >> rows; //Row and column are the iterators and counter to print int numberOfRows, numberOfColumns, counter = 0; //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 number //Iterator iterates from 1 to the numberOfRows for (numberOfColumns = 1; numberOfColumns <= numberOfRows; ++numberOfColumns) { cout << ++counter << " "; } //Prints a newline cout << endl; } return 0; }
Output: Rows : 4 1 2 3 4 5 6 7 8 9 10
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: