Print Reverse Floyd’s Triangle Number Pattern
In the previous article, we have discussed Java Program to Print Floyd’s Triangle Number Pattern
In this article we are going to see how to print reverse Floyd’s triangle number pattern.
- Java Code to Print Reverse Floyd’s Triangle Number Pattern
- C Code to Print Reverse Floyd’s Triangle Number Pattern
- C++ Code to Print Reverse Floyd’s Triangle Number Pattern
- NAUKRI Pivot Point Calculator
Example-1 When rows value = 5 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Example-2: When rows value=7 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Now, let’s see the actual program to print it.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
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 Reverse Floyd’s Triangle Number Pattern
import java.util.Scanner; class pattern { //Function to set the counter static int calculateCounter(int rows) { int sum = 0; for (int i = 1; i <= rows; i++) { sum += i; } return sum; } //Main Function 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 = calculateCounter(rows); //Outer loop to iterate the rows //Iterates from 1 to the number of rows entered by the user(backwards) for (numberOfRows = rows; numberOfRows >= 1; --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 : 5 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
C Code to Print Reverse Floyd’s Triangle Number Pattern
#include <stdio.h> //Function to set the counter int calculateCounter(int rows) { int sum = 0; for (int i = 1; i <= rows; i++) { sum += i; } return sum; } //Main Function 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 = calculateCounter(rows); //Outer loop to iterate the rows //Iterates from 1 to the number of rows entered by the user(backwards) for (numberOfRows = rows; numberOfRows >= 1; --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 : 5 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
C++ Code to Print Reverse Floyd’s Triangle Number Pattern
#include <iostream> using namespace std; //Function to set the counter int calculateCounter(int rows) { int sum = 0; for (int i = 1; i <= rows; i++) { sum += i; } return sum; } //Main Function 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 = calculateCounter(rows); //Outer loop to iterate the rows //Iterates from 1 to the number of rows entered by the user(backwards) for (numberOfRows = rows; numberOfRows >= 1; --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 : 5 15 14 13 12 11 10 9 8 7 6 5 4 3 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: