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

Time:03-12

        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.

CodePudding user response:

I am unsure what language this is in, but I would expect the division operator (/) to take precedence over the addition operator ( ), so in the first iteration of the loop you will indeed be performing a division by 0 which is what the error is telling you.

CodePudding user response:

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 ().

  • Related