Coders Packet

Inverted Number Pattern in Java

By Jasdeep Singh

We will be given an input number N, then we have to print the given pattern corresponding to that number. We have to write this code using Java language.

Understanding the problem

In this problem will be given an input number n then we have to print the pattern corresponding to that number n using java programming language. Different types of patterns can be created in java but here we will be specifically looking at printing reverse number pattern which is very basic pattern and easy to understand.We will be taking the input n and then will return the pattern.

For example, if N=5 Pattern output: 1

21
321
4321
54321

Solution

public class reversenumberpattern {

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    s.close();
    int currRow = 1;
    while(currRow <=n) {
      int currCol = currRow;
    
      while(currCol >=1) {
        System.out.print(currCol);
        currCol -= 1;
      }
      System.out.println();
      currRow += 1;
    }
    

  }


Output

5
1
21
321
4321
54321

How to approach?

1.Take N as input from the user.

2.Figure out the number of rows, (which is N here) and run a loop for that.

3.Now, figure out how many columns are to be printed in ith row and run a loop for thatwithin this.4.Now, find out “What to print?” in a particular row, column number. It can depend on the column number, row number or N. Here, it is N-i+1.

Download Complete Code

Comments

No comments yet