write a loop profram in java to print the following series:-
15 14 13 12 11
7 8 9 10
6 5 4
3 2
1
Answers
Pattern in Java
We have a decreasing sequence of numbers in a triangle pattern.
Suppose there are n rows. Then there are a total of elements.
[This is because the number of elements = 1+2+3+...+n = ]
We run a loop with a variable i for n rows. And we have an inner loop which runs i times in each row. Alongside we keep on printing the pattern elements.
Here is a Java program:
public class Pattern
{
public static void main(String[] args)
{
int n = 5; //Number of Rows
int k = (n*(n+1))/2; //First Element in first row
for(int i=n;i>0;i--) //Loop to print n rows
{
for(int j=0;j<i;j++) //Loop to print i elements in each row
{
System.out.print(k+" "); //Printing k
k--; //Decrementing k
}
System.out.println(); //Printing a new line after completion of a row
}
}
}
Answer:
123456789101112131415