Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

...............The numbers should line up ......1 ....so that the ones start sta

ID: 3544564 • Letter: #

Question




...............The numbers should line up ......1

....so that the ones start stacked on...... 2 1

top of each other on the right...etc ......3 2 1

.....................................................4 3 2 1

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

like that. This chat box won't let me type the pattern the way it should appear very well.
...
N n-1 3 2 1

The method header is
public static void displayPattern(int n)


Please give me the src code for the whole program. This is what I have and the pattern doesn't come out right:



import java.util.*;
public class exercise5_6{

public static void main(String[] args){



int n;

Scanner in=new Scanner(System.in);
System.out.println("Enter a number: ");

n=in.nextInt();
displayPattern(n);
}
public static void displayPattern(int n){
int i, j;
for(i=1; i<=n; i++)
{
for(j=i; j>=1; j--)

System.out.print(" " + j);
System.out.println();
}

}
}



Thanks

Explanation / Answer

/* package whatever; // don't place package name! */



import java.util.*;


import java.lang.*;


import java.io.*;


/* Name of the class has to be "Main" only if the class is public. */


class Ideone

{

public static void main (String[] args) throws java.lang.Exception

{


int n;

Scanner in=new Scanner(System.in);

System.out.println("Enter a no: ");

n=in.nextInt();

displayPattern(n);

}

public static void displayPattern(int n){

int i, j;

for(i=1; i<=n; i++)

{

for(j=0; j<=n+1-i; j++)

System.out.print(" "); //Here you have to leave 2 spaces then check output ...


for(j=i; j>=1; j--)

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

System.out.println();

}

}

}