Suppose I have an array
A[][] = {{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12}};
And I want to print a wave so that the output comes out like this
{1,5,9,10,6,2,3,7,11,12,8,4}
How can I do this??
here is my code but it is giving me ArrayIndexOutOfBound
public class Wave1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
System.out.println("Row "+a.length);
for(int i=0;i<a.length;i++){
System.out.println("Column "+i+"th "+a[i].length);
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
}
System.out.println();
for(int i=0;i<a.length+1;i++){
if(i%2==0){
for(int j=0;j<a.length;j++){
System.out.print(a[j][i]+" ");
}
}
else{
for(int j=a.length-1;j>=0;j--){
System.out.print(a[j][i]+" ");
}
}
}
Thanks in advance
It seems what you want is to print each column of the matrix, where the even indexed columns are printed in ascending order and the odd indexed columns are printed in descending order.
for (int col = 0; col < a[0].length; col++) {
if (col % 2 == 0) {
for (int row = 0; row < a.length; row++)
System.out.print(a[row][col] + " ");
System.out.println();
} else {
for (int row = a.length - 1; row >= 0; row--)
System.out.print(a[row][col] + " ");
System.out.println();
}
}
Output :
1 5 9 13 17
18 14 10 6 2
3 7 11 15 19
20 16 12 8 4
You can do it easily by taking two for loops, one for forward scanning and another for backward scanning.
Here is the code snippet:
public static void main (String[] args)
{
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};
for(int k = 0;k < 4;k++) {
for(int i = 0;i < a.length;i++) {
System.out.print(a[i][k] + " ");
}
k++;
for(int i = a.length - 1;i >=0 ;i--) {
System.out.print(a[i][k] + " ");
}
}
}
Output:
1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4
That's like traversing an array similiar to traversing a tree in zigzag traversal, but in this case we do not need stacks just modular arithmetich will be enough, here is your solution;
Method #1 - dummyPrint
private static void dummyPrint(int[][] array) {
System.out.print("Dummy Print: ");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
}
System.out.println();
}
Method #2 - prettyPrint
private static void prettyPrint(int[][] array) {
System.out.println("Pretty Print;");
System.out.println("*************");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
System.out.println();
}
}
Demonstration Code
public class ArrayDemo {
public static void main(String[] args) {
int [][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
dummyPrint(array);
System.out.println();
prettyPrint(array);
}
private static void dummyPrint(int[][] array) {
System.out.print("Dummy Print: ");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
}
System.out.println();
}
private static void prettyPrint(int[][] array) {
System.out.println("Pretty Print;");
System.out.println("*************");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
System.out.println();
}
}
}
The Output
Dummy Print: 1 5 9 10 6 2 3 7 11 12 8 4
Pretty Print;
*************
1 5 9
10 6 2
3 7 11
12 8 4
There is a discrepancy with the code you print and the desired output. In your code, there is a line that prints "row" and another that prints "column".
This code
public class Wave1 {
public static void main(String[] args) {
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
String separator="";
System.out.print("{");
for (int[] row: a) {
for (int cell: row) {
System.out.print(separator+cell);
separator=",";
}
}
System.out.println("}");
}
}
prints
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
I was not able to figure out the order in which the numbers should be printed. I assumed it did not matter.
public class Wave1 {
public static void main(String args[])
{
int [][] wave={{1,2,3,4},{5,6,7,8},{8,9,10,11},{12,13,14,15}};
for(int i=0;i<wave.length;i++){
if(i%2==0){
for(int j=0;j<wave[0].length;j++){
System.out.print(wave[i][j]+" ");
}
System.out.println();
}
else{
for(int j=wave[0].length-1;j>=0;j--){
System.out.print(wave[i][j]+" ");
}
System.out.println();
}
}
}
}
public static void wave(int[][]input)
{
int row=0;
int col=0;
int steps=1;
System.out.print(input[0][0]+" ");
while(steps<=input.length*(input[0].length)-1)
{
if(col%2==1)
{
row--;
}
else
{
row++;
}
if(row<0)
{
row++;
if(col%2==1)
{
col++;
}
}
if(row>input.length-1)
{
row--;
if(col%2==0)
{
col++;
}
}
System.out.print(input[row][col]+" ");
steps++;
}
}
Check this one:
Use this function it will take 2D Array as its input and would print a zigzag Path
public static void wavePrint(int input[][]){
int count = 1;
int rows = input.length;
int cols = input[0].length;
for(int i = 0; i < cols; i++){
if(count % 2 != 0){ //odd column
int sine_e = 0;
while(sine_e < rows){
System.out.print(input[sine_e][i]+" ");
sine_e++;
}
count++;
}else{
int sine_e = rows - 1; //even column
while(sine_e >= 0){
System.out.print(input[sine_e][i]+" ");
sine_e--;
}
count++;
}
}//end of for loop
}
Related
So I'm trying to create a program that creates a randomly generated array with numbers between 0 and 10.
Every time a number inside the 4x4 array is odd I want it to generate a brand new array and print every array discarded aswell until it creates a 4x4 array with only even numbers.
The problem right now is that I can't understand how to fix the last for and make it work properly with the boolean b that is supposed to restart the creation of the array.
import java.util.Scanner;
public class EvenArrayGenerator {
public static void main(String a[]) {
Boolean b;
do {
b = true;
int[][] Array = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
Array[i][j] = (int) (Math.random() * 11);
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (Array[i][j] % 2 != 0)
b = false;
}
}
} while (b);
}
}
public class ArrayGen {
private int[][] array = new int[4][4];
private int iterations = 1; // you always start with one iteration
public static void main (String[] args) {
ArrayGen ag = new ArrayGen();
ag.reScramble();
while(!ag.isAllEven()) {
ag.reScramble();
ag.iterations++;
}
// this is just a nice visualisation
for (int i = 0; i < 4; i++) {
System.out.print("[");
for (int j = 0; j < 4; j++) {
System.out.print(ag.array[i][j] +((j != 3)? ", " : ""));
}
System.out.print("]\n");
}
System.out.println(ag.iterations + " iterations needed to get all-even array.");
}
private void reScramble () {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
array[i][j] = (int)(Math.random() * 11);
}
}
}
private boolean isAllEven () {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (array[i][j] % 2 == 1) {
return false;
}
}
}
return true;
}
}
I think this is a good solution. Refactoring your code into structured methods is never a bad idea. I hope this helps!
You are looping until you get an array that's all even. You should initialize b to be false, and update it to true in the (nested) for loop. Note that once's you've set it to false, there's no reason checking the other members of the array, and you can break out of the for loop.
Note, also, that using stream could make this check a tad more elegant:
b = Arrays.stream(arr).flatMapToInt(Arrays::stream).anyMatch(x -> x % 2 != 0)
What about generating random numbers up to 5 and double it? Then you don't have two check if they are even.
Instead of your last for loop:
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(Array[i][j] % 2!=0){
b=false;
break;
}
}
if(!b){
break;
}
}
if(!b){
break;
}
Alternatively, you could do an oddity check when you are generating the elements. Something like:
int element;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
do{
element = (int)(Math.random()*11);
}while(element % 2 !=0)
Array[i][j] = element;
}
}
That way you don't have to check the values, they will always be even.
This should work:
import java.util.Scanner;
public class EvenArrayGenerator{
public static void main(String a[]){
boolean anyOdd;
int array = 0;
do{
System.out.println ("Array " + ++array + ":");
anyOdd=false;
int[][] Array = new int[4][4];
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
Array[i][j] = (int)(Math.random()*11);
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
anyOdd |= Array[i][j] % 2!=0;
}
}
} while(anyOdd);
}
}
As you can see, I just modified the condition from b to anyOdd, so if there is any odd number, it will iterate again.
Also, you can check it when you generate the random numbers, so you avoid a second loop:
import java.util.Scanner;
public class EvenArrayGenerator{
public static void main(String a[]){
boolean anyOdd;
int array = 0;
do{
System.out.println ("Array " + ++array + ":");
anyOdd=false;
int[][] Array = new int[4][4];
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
Array[i][j] = (int)(Math.random()*11);
anyOdd |= array[i][j] % 2 != 0;
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
} while(anyOdd);
}
}
public class EvenArrayGenerator {
public static void main(String a[]) {
int[][] arr = createAllEvenArray(4);
printArray(arr);
}
private static int[][] createAllEvenArray(int size) {
while (true) {
int[][] arr = createArray(size);
printArray(arr);
if (isAllEven(arr))
return arr;
}
}
private static int[][] createArray(int size) {
int[][] arr = new int[size][size];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr.length; j++)
arr[i][j] = (int)(Math.random() * 11);
return arr;
}
private static void printArray(int[][] arr) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j > 0)
System.out.print("\t");
System.out.format("%2d", arr[i][j]);
}
System.out.println();
}
System.out.println();
}
private static boolean isAllEven(int[][] arr) {
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr.length; j++)
if (arr[i][j] % 2 != 0)
return false;
return true;
}
}
I want to break a one-dimensional array in rows.
Array dimension 50. I need to output the array to the console with 10 elements per line. (lang Java 1.8) Thanks!
public void print() {
for (int i = 0; i < arr.length; i++) {
if (i<=9) {
System.out.print(arr[i] + " ");
}else {
System.out.print("\r");
}
}
}
Sample output
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
etc....
You can see it from 2 differents point of view
Each 10 numbers, print a new line : when the index ends with a 9 you reach ten elements so you print a new line println()
public void print() {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
if (i % 10 == 9) {
System.out.println();
}
}
}
Print enough number of line and on each one : print 10 elements
public void print() {
for (int i = 0; i < arr.length / 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(arr[i * 10 + j] + " ");
}
System.out.println();
}
}
Code for any number of elements per line:
public void print(int elementsPerLine) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i % elementsPerLine == 0 && i > 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
}
Use the following code,
public static void printResult(int[][] result)
{
for(int i=0; i<5; i++)
{
for(int j=0; j<10; j++)
{
System.out.print(result[i][j] + ", ");
}
System.out.println();
}
}
public static int[][] modifyArray( int[] singleArray )
{
int columns = 10;
int rows = singleArray.length/10;
int[][] result = new int[rows][columns];
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
result[i][j] = singleArray[columns*i + j];
}
}
return result;
}
public static void main(String[] args)
{
int[] singleArray = {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,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
int[][] result = modifyArray( singleArray);
printResult( result );
}
You must use the modulo operator
https://en.wikipedia.org/wiki/Modulo_operation
public void print() {
for (int i = 0; i < arr.length; i++) {
if (i%10 == 0) {
System.out.print("\r");
}else {
System.out.print(arr[i] + " ");
}
}
}
Maybe you could write something like
if (i % 10 == 0)
System.out.print("\r");
}
System.out.print(arr[i] + " ");
Take slices of 10 items into a new array each time and print that array:
int count = 10;
int iterations = (arr.length / count) + ((arr.length % count) > 0 ? 1 : 0);
for (int i = 1; i <= iterations; i++) {
int[] slice = new int[count];
System.arraycopy(arr, (i - 1) * count, slice, 0, i == iterations ? (array.length % count) : count);
System.out.println(Arrays.toString(slice));
}
The above code works for any value of count.
Take a look at a code down below:
public class PrintEach10thElements {
/**
* #param args the command line arguments
*/
static List<Integer> arrays = new ArrayList<>();
public static void main(String[] args) {
//create 50 elements in an array
for (int i = 0; i <= 49; i++) {
arrays.add(i);
}
for (int g = 0; g < arrays.size(); g++) {
if ( arrays.get(g) % 10 != 0) {
System.out.print(arrays.get(g) + " ");
} else {
System.out.println();
System.out.print(arrays.get(g) + " ");
}
}
}
}
If you don't mind using a Guava library, you can also do this
List<Integer> myList = Arrays.asList(myArray);
System.out.println(Lists.partition(myList, 10).stream()
.map(subList -> subList.stream()
.map( Object::toString )
.collect(Collectors.joining(" ")))
.collect(Collectors.joining("\n")));
I am trying to make a program that outputs
So far I have done:
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
System.out.println("0");
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
max++;
}
}
}
But the output I am getting is
The "0" is there to show the spaces, but I want to remove the entire line which contains the first "0" so that the output starts with a "1". I am unsure what to change. Any help would be much appreciated. Thank You.
I suggest adding conditions (if we need to print out delimiters):
for (int i = 1; i <= a; ++i) {
if (i > 1)
System.out.println(); // more than 1 line, need delimiter (new line)
for (int j = 1; j <= i; ++j) {
if (j > 1)
System.out.print(" "); // more than 1 column, need delimiter (space)
System.out.print(j);
}
}
Most shortest form:
String str = "";
for (int i = 1; i <= 6; i++) {
str = str + " " + i;
System.out.println(str);
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Here what I've got
Here you can check https://code.sololearn.com/c9ALHSGAa6ZZ
class printPattern {
public static void main(String[ ] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for (i = 1; i <= a; i++) {
num = 1;
for (j = 1; j <= max; j++) {
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println();
max++;
}
}
}
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println(" ");
max++;
}
}
}
this is working as you asked. just remove 0 from print statement
How about this ?
public void pyramid(int size) {
for(int i = 1; i <= size; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}
}
Im sorry for bothering for this small thing but really i feel confused about this. I want to get output like;
9
89
789
6789
56789
456789
3456789
23456789
123456789
23456789
3456789
456789
56789
6789
789
89
9
But im taking my output after 123456789 line. My codes are;
for(int column = 1; column <= 9; column++) {
for(int row = 1; row <= 9; row++) {
if(column <= row) {
System.out.print(row);
} else {
System.out.print(" ");
}
}
System.out.println(' ');
}
Thanks For Your Attention.
This is a bit dirty, but gives the desired output:
for(int column = -9; column <= 9; column++)
{
if (column == 0) column = 2;
for(int row = 1; row <= 9; row++)
{
if(Math.abs(column) <= row)
{
System.out.print(row);
} else
{
System.out.print(" ");
}
}
System.out.println();
}
This is a different approach that works
String txt = "123456789";
String msk = " ";
boolean done = false;
boolean pos = true;
int rows = 0;
int c = 0;
int maxrows=19;
while (!done) {
System.out.println(msk.substring(c) + txt.substring(9-c));
if (pos) {
c++;
if (c>8) pos = false;
} else {
c--;
if (c<1) pos = true;
}
rows++;
if (rows==maxrows) done = true;
}
Here's a simple way:
int num = 9;
int spaces = 8;
boolean decreasingSpaces = true;
for (int row=0; row < 17; row++) {
for (int i=0; i < spaces; i++)
System.out.print(" ");
for (int j=num; j <= 9; j++)
System.out.print(j);
System.out.println();
if (decreasingSpaces) {
spaces--;
num--;
if (spaces < 0) {
decreasingSpaces = false;
spaces += 2;
num += 2;
}
} else {
spaces++;
num++;
}
}
/*output:
54321
5432
543
54
5
*/
class NumberPyramid1
{
public static void main(String args[])
{
int n=5;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(" ");
}
for(int k=n;k>=i;k--){
System.out.print(k);
//n--;
}
System.out.println();
//n--;
}
}
}
for(int column = 1; column <= 9; column++) {
for(int row = 1; row <= 9; row++) {
if(column <= row) {
System.out.print(row);
} else {
System.out.print(" ");
}
}
System.out.println(); // println will create a newline
}
I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern 1
54321
5432
543
54
5
Pattern 2
1
12
123
1234
12345
Pattern 3
12345
2345
345
45
5
Pattern 4
1
123
12345
123
1
My code that almost matched pattern 1 is the following but doesn't work like the example above.
for (int i = 1 ; i <= rows ; i++) {
for (int j = (rows + 1 - i) ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.print("\n");
}
public class PrintPattern {
public static void main(String[] args){
printPattern1();
printPattern2();
printPattern3();
printPattern4();
}
public static void printPattern1(){
for (int i = 0; i<5; i++){
for(int j = 5; j>i; j--)
System.out.print(j);
System.out.println();
}
}
public static void printPattern2(){
for (int i = 0; i<5; i++){
for(int k = 0; k<4-i; k++)
System.out.print(" ");
for(int j = 1; j<=i+1; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern3(){
for (int i = 0; i<5; i++){
for(int k = 0; k<i; k++)
System.out.print(" ");
for(int j = i+1; j<=5; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern4(){
for (int i = 0; i<5; i++){
for(int k = 0; k<Math.abs(2-i); k++)
System.out.print(" ");
for(int j = 1; j<=5-2*Math.abs(2-i); j++)
System.out.print(j);
for (int p = 0; p<Math.abs(2-i); p++)
System.out.print(" ");
System.out.println();
}
}
}
Your inner for loop
for (int j = (rows + 1 - i) ; j > 0 ; j-- ){
System.out.print(j);
}
will always count down to 1, because it keeps going until j is zero. Also, the number that the current row starts on will depend on the current row, because you used i in your assignment to j. To get pattern 1 both of those things will have to change.
Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -
int rows = 5; // <- Start at 5.
for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
System.out.print(j);
}
System.out.println();
}
Output is pattern 1 in your question,
54321
5432
543
54
5
Try this:
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 2; k >= i; k--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 2; i++) {
for (int k = i; k > 0; k--) {
System.out.print(" ");
}
if (i % 2 != 0) {
for (int j = 1; j <= (2 * i + 1); j++) {
System.out.print(j);
}
} else {
for (int j = 1; j <= (i / 2); j++) {
System.out.print(j);
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
int rows = 5;
System.out.println("------ PATTERN 1 ------");
for (int i = 1 ; i <= rows ; i++){
for (int j = rows; j >= i ; j--){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 2 ------");
for (int i = 1 ; i <= rows ; i++){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 3 ------");
for (int i = rows ; i >= 1 ; i--){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 4 ------");
int whitespaces = rows/2;
for (int i = 1 ; i <= rows; i++){
// absolute value of whitespaces
int abs_whitespaces =
(whitespaces < 0 ? -whitespaces : whitespaces);
for (int j = 0 ; j < abs_whitespaces ; j++){
System.out.print(" ");
}
for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){
System.out.print(j);
}
whitespaces-=1;
System.out.println();
}
}
The first program is:
class timepass {
public static void main() {
for (int a = -1;a<=5;a++) {
for(int b = 5; b >= a;b--) {
System.out.print("*");
}
System.out.println();
// enter code here
}
}
}
The second program is:
class timepass {
public static void main() {
for(int i = 1;i<= 6;i++) {
for(int j = 1;j<= i ;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Triangle {
public static void main(String[ ] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter no.of line you need");
int n = scan.nextInt();
for(int i=1;i<=n;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println(" ");
}}}