0
Why error at coming at line temp= total%10; err msg: invalid Binary operands to binary expression
vector<int> sum(vector<int> A, int k){ int n= A.size(); double sum= 0; for(int i=0;i<n;i++){ sum= (sum+A[i])*10; } sum/=10; int temp_int = (int)sum; double total= temp_int+k; double temp; vector<int> s; while(total>0){ temp= total%10; total/=10; s.push_back(temp); } reverse(s.begin(),s.end()); return s; }
5 Answers
+ 1
Because you can only do modulus operations with integers.
+ 1
SHĹŞBHĂM, adding to Marcos Chamosa RodrĂguez's good answer, there is a library function that can do modulo with floating point, if that is your intention. See details for calling fmod() here:
https://cplusplus.com/reference/cmath/fmod/
And since you are using both, quotient and remainder, maybe div() or modf() would be more useful:
https://cplusplus.com/reference/cstdlib/div/?kw=Div
https://cplusplus.com/reference/cmath/modf/
+ 1
SHĹŞBHĂM long long can easily store 9999999999 and 1 more. If your program shows a wrong answer, then check for anyplace in the data path where it might be causing overflow in a smaller data type (i.e., getting copied into an int).
0
Brian I have used long long int and I'm not getting any error but for addition of long numbers like 9999999999+1 it's giving incorrect ans
0
Brian thanks for solution