Pascal's Triangle Format

前端 未结 8 1860
长发绾君心
长发绾君心 2020-12-03 08:38

The assignment is to create Pascal\'s Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 09:04

    public static void main(String[] args)
    {
        int a, num;
    
        for (int i = 0; i <= 4; i++) 
        {
            num = 1;
            a = i + 1;
    
            for(int j=4;j>0;j--)
            { 
                if(j>i)
                    System.out.print(" ");
            }
    
            for (int j = 0; j <= i; j++) 
            {
                if (j > 0) 
                    num = num * (a - j) / j;   
    
                System.out.print(num + " ");
            }
            System.out.println();
        } 
    }
    

    Code perfectly prints pascal triangle

提交回复
热议问题