1 Replies - 355 Views - Last Post: 18 February 2010 - 09:37 AM

#1 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Printing Pascal's Triangle

Posted 17 February 2010 - 04:10 PM

Description: This simple method prints to the console the first n lines of Pascal's Triangle.
public void pascalTriangle(int n){
    	
    	//looping number of lines
    	for(int i = 0 ; i < n; i++){
    		
    		//first value for each line
			int val = 1;
			
			//looping values per line
			for(int k = 0 ; k <= i; k++){
				
				//printing value
				System.out.print(val + "t");
				
				//calculating next value
				val = val * (i - k) / (k + 1);
				
			}
			
			//new line
			System.out.println();
			
    	}
    	
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Printing Pascal's Triangle

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Printing Pascal's Triangle

Posted 18 February 2010 - 09:37 AM

The only thing I would add is that this is left justified.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1