Print reverse pyramid of numbers

前端 未结 5 794
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 17:08

I am able to print pyramid like this :

   1
  123
 12345
1234567

Code i used to print the pyramid of numbers like a

相关标签:
5条回答
  • 2021-01-29 17:18

    Try this :)

     int a = 1;
                int b = 4;
                int m = 4;
    
                for (int i = 1; i <= 4; i++) {
                    for (int c = 1; c <= m; c++) {
                        text.append("  ");
                    }
                    for (int k = 1; k <= b; k++) {
                        String result = String.valueOf(k);
                        text.append(result);
                    }
                    a = a + 2;
                    b--;
                    m++;
                    text.append("\n");
                }
    
    0 讨论(0)
  • 2021-01-29 17:20

    Try this

    int a = 1;
    int b = 4;
    for (int i = 1; i <= 4; i++) {
        for (int k = a; k >= 1; k--) {
            String result = String.valueOf(k);
            text.append(result);
        }
        for (int c = 1; c <= b - 1; c++) {
            text.append("  ");
        }
        a = a + 2;
        b--;
        text.append("\n");
    }
    
    System.out.println(text.reverse());
    
    0 讨论(0)
  • 2021-01-29 17:21
            int numberofdigits = 7;
            int numberofdigitsforrow = 0;
            int Emptyspace = 0;
    
            for (int i = 0; i < numberofdigits; i++)
            {
                numberofdigitsforrow = numberofdigits - i * 2;
                if (numberofdigitsforrow > 0)
                {
                    Emptyspace = numberofdigits-numberofdigitsforrow;
                    if (Emptyspace > 0)
                    {
                        for (int b = 1; b <= Emptyspace/2; b++)
                        {
                            Console.Write(" ");
    
                        }
                    }
                    for (int c = 1; c <= numberofdigitsforrow; c++)
                    {
                        Console.Write(c.ToString());
                    }
                    if (Emptyspace > 0)
                    {
                        for (int b = 1; b <= Emptyspace / 2; b++)
                        {
                            Console.Write(" ");
                        }
                    }
                    Console.WriteLine();
                }
            }
    
    0 讨论(0)
  • 2021-01-29 17:22
    int w = 7;
            System.out.println("Here is your pattern....!!!");
    
            for (int i = 1; i <= 5; i++)
            {
                //Printing i spaces at the beginning of each row
    
                for (int j = 1; j < i; j++)
                {
                    System.out.print(" ");
                }
    
                //Printing i to rows value at the end of each row
    
                for (int j = 1; j <=w; j++)
                {
                    System.out.print(j+" ");
                }
                w = w - 2;
    
                System.out.println();
            }
    
    0 讨论(0)
  • 2021-01-29 17:29

    try this ...

     for (int i = 7; i > 0; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.print("\n");
    
            if (i % 2 != 0) {
                i = i - 1;
            }
        }
    
    0 讨论(0)
提交回复
热议问题