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

I am writing a java code that takes a user input and makes the number pattern; 1

ID: 3676606 • Letter: I

Question

I am writing a java code that takes a user input and makes the number pattern;

1 1

12 21

123 321

1234 4321

and so on and so forth for what ever the user put in as the line number, however my code does not do that, if anyone could look over my code and tell me what im doing wrong and point me in the right direction I would greatly appreciate the help.

import java.util.Scanner;

public class numPatterns{
public static void main (String []args){
Scanner scnr = new Scanner(System.in);
int i,j,k;
  
System.out.println("enter number of lines to print:");
k = scnr.nextInt();
  
for (i=1;i<=k;i++) {
  
for(j=1; j<=k;j++) {
if(j>=i)
System.out.print(j);
else
System.out.print(" ");
}
for (j=k; j>=i; j--)
{
if(j>=i)
System.out.print(j);
else
System.out.print(" ");
}
System.out.println();
}
  
}
}
  

Explanation / Answer

/**The java program numPatterns that prompts user to
* enter the number of lines to print to
* write a pattern to console
* 1 1
* 12 21
* 123 321
* 1234 4321*/
//numPatterns.java
import java.util.Scanner;
public class numPatterns
{
   public static void main (String []args)
   {
       //Create a Scanner class object
       Scanner scnr = new Scanner(System.in);
       int i,j,k;

       System.out.println("enter number of lines to print:");
       //read number of lines
       k = scnr.nextInt();

       //run for loop that run from i to k
       for (i=1;i<=k;i++)
       {

           //print from j=1 to j<=i
           for(j=1; j<=i;j++)
           {              
               System.out.print(j);
           }
          
           System.out.print(" ");
          
           //print from j=i to j>=1
           for (j=i; j>=1; j--)
           {
               System.out.print(j);
           }

           //print a new line
           System.out.println();
       }

   }//end of the main method
  
}//end of the class

--------------------------------------------------------------------------------------------------------------------------------------------

Sample output:

enter number of lines to print:
4
1 1
12 21
123 321
1234 4321

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote