Search code examples
arraysruntimepascals-triangle

I'm getting the error : runtime error: division by zero ; for my code Can someone plzz help me


        int j;
        for( j=0;j<=i;j++)
        {  v.push_back(val);
        val= val*(i-j)/j+1;
        }   
   return v;

In the line val=val*(i-j)/j+1 ; the error is occuring.


Solution

  • Just change your code to this-

    int j;
        for( j=0;j<=i;j++){  
            v.push_back(val);
            val= val*(i-j)/(j+1);
        }   
    return v;
    

    This error was occurring because you were dividing the value with j in line 4 that was initially 0, however you were adding one but you should have used ().