Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2010
    Location
    Canada
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Pascal's Triangle

    I can't find the error's in this program. I had to create these methods and must use them. It's for my high school class and I've cut 19 errors to one.

    Code:
    import java.util.Scanner;
    
    public class Pascal{
    
    public static void main(String[] args)
    {
    	Scanner input= new Scanner(System.in);
       int rows = getRows();
       while (rows != 0){
          printTriangle(rows);
          rows = getRows();
       }
    } 
    
    public static long factorial(long n)
    {
    	//A number that is leading up to the specified number
    	for (int i=2,k=1; i<=n; i++){
    	k*=i;
    	return k;
    	}
    	return 1;
    }
    public static int getRows(){
    	Scanner input= new Scanner(System.in);
    	
    	int rows=5;
    	System.out.println("Please enter the number of rows:");
    	rows = input.nextInt();
    	return (rows);
    }
    
    public static long nCr(long n, long r){
    	return factorial(n)/factorial(r)*(factorial (n-r));
    }
    
    public static void printTriangle(int r){
    	for (int i=1;i<=r;i++)
    	{
    		for(int j=1;j<=i; j++)
    		{
    			System.out.println(nCr(i-1,j-1));
    		}
    		
    		System.out.println("");
    	  }
    
    	}
    
    }

  • #2
    New to the CF scene
    Join Date
    Mar 2011
    Location
    san antonio,TX
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb error

    what did the compiler spit out, what type of error?

  • #3
    New Coder
    Join Date
    Jul 2010
    Location
    Canada
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by barillitos View Post
    what did the compiler spit out, what type of error?
    Arithmetic exception..divide by zero?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •