0

I want to print number pattern like this..

but, I failed to get this triangle shape,and I am confused how to put spaces to get this shape -->

   1
  212
 32123
4321234

Here's the code i tried so far

public class Ch {

    public static void main(String[] args) {
        int r =Integer.parseInt(args[0]);
        for(int u=1;u<=r;u++)
        { 
            for(int i=u;i>=1;i--)
            {
                System.out.print(i);
            }

            for(int i=2;i<=u;i++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

output of this code looks-like this

     1
     212
     32123
     4321234

Thanks

Neuron
  • 3,776
  • 3
  • 24
  • 44
Ehan
  • 1

2 Answers2

1

Just add one more step in you main loop before the other two:

for (int i = u; i < r; i++)
{
    System.out.print(" ");
}

This will print spaces to make up for the "missing" numbers.

In regard to Mateusz' comment, look at this answer on how to pad your numbers with spaces to make them equally wide in case you go beyond 9:

static int padding;

public static void main(String[] args)
{
    int r = Integer.parseInt(args[0]);

    padding = Math.max(1, (int) Math.ceil(Math.log10(r)));

    for (int u = 1; u <= r; u++)
    {
        for (int i = u; i < r; i++)
        {
            print(" ");
        }

        for (int i = u; i >= 1; i--)
        {
            print(i);
        }

        for (int i = 2; i <= u; i++)
        {
            print(i);
        }
        System.out.println();
    }
}
private static void print(Object text)
{
    System.out.print(String.format("%1$" + padding + "s", text));
}
Malte Hartwig
  • 4,206
  • 2
  • 11
  • 30
0

using 2 for loops

package com.stackoverflow;

import java.util.Scanner;

public class Pyramid {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the pyramid limit value: ");
        int limit = in.nextInt();
        in.close();
        for (int i = 0; i < limit; i++) {
            for (int j = 0; j < limit + i; j++) {
                if (j < limit -i-1)
                    System.out.print(" ");
                else{               
                    int temp = (limit-j > 0) ? limit-j : j-limit+2;
                    System.out.print(temp);
                }
            }
            System.out.println();
        }
    }

}

using 4 for loops

package com.stackoverflow;

import java.util.Scanner;

public class Pyramid {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the pyramid limit value: ");
        int limit = in.nextInt();
        in.close();
        for (int i = 0; i < limit; i++) {
            for(int j=0; j<limit-i; j++){
                System.out.print(" ");
            }
            for(int j=0; j<=i; j++){
                System.out.print(i-j+1);
            }
            for(int j=i; j>0; j--){
                System.out.print(i-j+2);
            }
            System.out.println();
        }
    }
}
trinath
  • 249
  • 2
  • 15