Computer Science, asked by Anonymous, 7 months ago

Write a java program for the following output :-

1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

Answers

Answered by Anonymous
19

refer to the attachment

mark as brainliest

thank you

gd eve

Attachments:
Answered by anindyaadhikari13
3

Question:-

Write a Java program to display the following pattern.

1 2 3 4 5 4 3 2 1

1 2 3 4 3 2 1

1 2 3 2 1

1 2 1

1

Program:-

Here is your program written in Java.

import java.util.*;

class Pattern

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.print("Enter the number of rows for the pattern: ");

int n=sc.nextInt();

for(int i=n;i>=1;i--)

{

int a=1, b=1;

for(int j=1;j<=2*i-1;j++)

{

System.out.print(a+" ");

a+=b;

if(a>i)

{

b=-b;

a-=2;

}// end of if.

}// end of inner loop.

System.out.println();

}// end of outer loop

}// end of main()

}// end of class

Similar questions