troubleshooting Question

How do i handly doing a multiplying by 0 in JAVA

Avatar of awolarczuk
awolarczukFlag for Australia asked on
JavaLinux Distributions
21 Comments1 Solution353 ViewsLast Modified:
Hi there all
 there will be times in my JAva app i am writting that i will need to multiplying by 0, it seems that when i run this is am getting a error i was hoping someone could please help me with this

Exception in thread "main" java.lang.ArithmeticException: / by zero
      at pascal.value(pascal.java:68)
      at pascal.drawTriangle(pascal.java:93)
      at PascalDemo.main(PascalDemo.java:41
//creating the mothod for Pascal


/** This is a method for testing that my if statment is working fine */

public class pascal
{

   
   
  
    //* This Method will calculate the factoral of a positive number (including 0) and return it */
    private static int factorial(int number)  
    {
	

	if (number == 0)
	{
	    return 0;
	}
	else
	    {
		long workingTotal = 1;                               //storing a working total for the factotial 
                                    
		//	System.out.println (number);
		for (int count = 1;count <= number; count++)       // working out the factorial
		    {
			workingTotal = workingTotal * count;
		   
		    }
	 
	  
		int change = (int)workingTotal;                     //change the workingTotal back to a Int to be passed back to Value
	
	  
	
		return change;
	    }
	
    }

    

    //* This is the Method that  calculate the binomial coefficients, it will call the factorial method when needed */ 
    private static int value(int n, int k) 
    {
	long a;                                              //part one of the binomial coefficient, this will be storning the n!
	long b;                                              //part two of the binomial coefficient, this will be storning the k!
	long c;                                              //part three of the binomial coefficient, this will calulate n-k and get the total after it is passed to to the to make a factorial number
	long d;                                              //this will store the k * (n-k)!
	long bico;                                           // this is to store the binomial coefficient
	int e;
	int f;
	
	k = 0;

	for (int count = 0;count <= n; count++)	
	    {
		a = factorial(n);                           //passing the n! from value to factorial to work out the facorial number for n!
			System.out.println ("\nfactorial N of " + n + " is " + a);
		b = factorial(k);                            //passing the k! from value to factorial to work out the facorial number for k!    
		System.out.println ("\nfactorial k of " + k + " is " + b);
                              
		c = factorial(n - k);
		System.out.println ("\nfactorial n - k  " + n + " -  " + k + " = " + c);
		d = b * c;
		System.out.println ("\nk*( n - k)!  " + b  + " *  " + c  + " = " + d);
		bico = a / d;
		System.out.println ("\binomial coefficient is  " + a  + " /  " + d  + " = " + bico);
	       
		e = (int)a;                                    //converting the long a to double
		f =(int)d;                                    //converting the long d to a double
	
		//	System.out.println ("fac of " + n + " is " + a);	
		//	System.out.println ("fac of " + k + " is " + b);

		k++;                                         //increacing the k count
		//	bico = (a/d);
		// System.out.print (bico);
	
	       
	    }

	 
	return (0 );


    }

    //* This Method is to handle the height of the triangle
    public static void drawTriangle(int numberSelected)
    {
	value(numberSelected,0);
	
    }
    

}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 21 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 21 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros