Why error at coming at line temp= total%10; err msg: invalid Binary operands to binary expression | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

8th Aug 2023, 3:13 PM
SHŪBHÃM
SHŪBHÃM - avatar
5 Answers
+ 1
Because you can only do modulus operations with integers.
8th Aug 2023, 6:34 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
+ 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/
8th Aug 2023, 8:14 PM
Brian
Brian - avatar
+ 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).
9th Aug 2023, 4:20 AM
Brian
Brian - avatar
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
9th Aug 2023, 2:49 AM
SHŪBHÃM
SHŪBHÃM - avatar
0
Brian thanks for solution
9th Aug 2023, 7:04 AM
SHŪBHÃM
SHŪBHÃM - avatar