'How To Print The Numbers Backwards So That Output pattern is like a triangle

I have to write a program so that it prints a triangle like the following:

If the user gives Input 3, the output should be:

  3
 23
123

If the user gives Input 4, the output should be:

   4
  34
 234
1234

So I wrote the following code:

Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows");
int row=sc.nextInt();

for(int maincount=1; maincount<=row;maincount++){
    for(int spcount=1;spcount<=row-maincount;spcount++){
        System.out.print(" ");
    }
    //int numprint=row;            
    for(int numcount=1;numcount<=maincount;numcount++){
        System.out.print(/*numprint*/numcount);
        //numprint--;                
    }
        System.out.println("");
    }

But the output to the code is something like this: If I input 3:

  1
 12
123

It looks very close to the output, so tried to find the problem

I used another variable "numprint" instead of "numcount". (I commented out The parts where I used numprint); But this time the output was:

  3
 32
321

The alternatives I came up with I couldn't find any working solution that would show an output similar to the desired one.



Solution 1:[1]

I think this is what you want:

Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows");
int row=sc.nextInt();

for (int i=1; i<row+1; i++) {
    for (int j=1; j<row+1; j++) {
        if (j > row - i) {
            System.out.print(j);
        } else {
            System.out.print(" ");
        }
    }
    System.out.println();
}

The outer iteration using i assures 2 things:

  • To print the sequence of numbers using the inner iteration
  • Jump on the next line using System.out.println();

The inner iteration using j assures only printing the sequence of numbers. The key is to determine whether print the empty space " " or the number. j > row - i is the condition.

Solution 2:[2]

You can try this

        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number of rows");
        int row=sc.nextInt();

        for (int i = 1; i <= row; i++) {
            for(int j = 0; j < row - i; j++) {
                 System.out.print(" ");
            }
            int temp = row - (i -1);
            for (int j = 0; j < i; j++) {
                System.out.print(temp++);
            }
            System.out.println("");
        }

Solution 3:[3]

Try this:-

        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number of rows");
        int row=sc.nextInt();

        for (int maincount = 1; maincount <= row; maincount++) {
            for (int spcount = 1; spcount <= row - maincount; spcount++) {
                System.out.print(" ");
            }
            int numprint = row;
            String str = "";
            for (int numcount = 1; numcount <= maincount; numcount++) {
                str = numprint + str;
                numprint--;
            }
            System.out.print(str);
            System.out.println("");
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Pawan Singh
Solution 3 Ravindra