Plzzz answer what will be the programming in java for this
Answers
Java Pattern
In Java pattern questions we first figure out how to set the loops. Here we can look at the figure in two parts. One part is of the increasing length of rows. And the other part is of the rows with decreasing length.
So we use two loops, one increasing and the other decreasing.
Now, on each row, numbers increase to a limit and then decrease back. So we use two loops inside each of the main outer loops.
Here is a Java program for printing the pattern.
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value for n: ");
int n=sc.nextInt(); //Taking User Input for n
for(int i=0;i<n;i++) //First Loop runs for increasing length of rows
{
for(int j=0;j<=i;j++) //Loop for printing the increasing numbers on each row
{
System.out.print(j+1);
}
for(int j=i;j>0;j--) //Loop for printing the decreasing numbers on each row
{
System.out.print(j);
}
System.out.println(); //Printing a blank line after a row is completed
}
//The same above loop of i is repeated with a different range for decreasing length of rows
//All inner contents are the same
for(int i=n-2;i>=0;i--)
{
for(int j=0;j<=i;j++)
{
System.out.print(j+1);
}
for(int j=i;j>0;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}